question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get Where parameters from IQueryable

See original GitHub issue

I’m writing a Update extension with dynamicly input fields and it’s need to complete my update query with “where” paramters of IQueryabale source … what is way out, please! maybe that could be done in a different manner ? thanks!

… query = query.UpdateAll(properties_collection); …

public static IQueryable UpdateAll(this IQueryable source, object fields_collection) { var pairs = new List<string>(); var parameters = new Dictionary<string, object>();

        var safety_fields = GetSafeFields(fields_collection);
        var table = source.GetType().GetGenericArguments()[0].Name;

        foreach(var field in safety_fields)
        {
            var value = fields_collection.GetType().GetProperty(field).GetValue(fields_collection, null);
            var value_safe = value.IsNumericType() ? value.ToString() : value.ToLiteral();
            parameters.Add(field, value_safe);
        }

        /////////////////////////////////////////////////////////////

        IDbCommand UpdateCmd = SingletonDatabase.dbContext.CreateCommand();
        StringBuilder builder = new StringBuilder(String.Format("UPDATE {0} SET ", table));

        foreach (KeyValuePair<string, object> parameter in parameters)
        {
            builder.Append(parameter.Key).Append(" = @").Append(parameter.Key).Append(",");

            var dp = UpdateCmd.CreateParameter();
                dp.ParameterName = "@" + parameter.Key;
                dp.Value = parameter.Value;
                UpdateCmd.Parameters.Add(dp);
        }

        builder.Remove(builder.Length - 1, 1);
        builder.Append(" WHERE ");

         /////////////////////////
        ?? how to get source where paramters to complete query ??
        ////////////////////////////

        //set the command text and execute the command
        UpdateCmd.CommandText = builder.ToString();

        Console.WriteLine("B:{0}", builder.ToString());
        //UpdateCmd.ExecuteNonQuery();

        return source;
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
zgandrewcommented, Sep 13, 2016

sdanyliv it’s INCREDIBLE, works as sharm !! a lot of thanks for the excelent support!!!

0reactions
sdanylivcommented, Sep 13, 2016

You can find me in skype svyatoslav.danyliv, i’m also from Ukraine

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrieve LINQ to sql statement (IQueryable) WITH ...
I'm using Datacontext.Log property to get the generated SQL Statement (it includes the statement text, and parameters).
Read more >
Queryable.Where Method (System.Linq)
The query behavior that occurs as a result of executing an expression tree that represents calling Where<TSource>(IQueryable<TSource>, Expression<Func<TSource, ...
Read more >
C# .NET: Get SQL String With Parameters from IQueryable ...
After many many hours, I have finally figured out how to take an IQueryable object and get the actual SQL statement and parameters....
Read more >
DataContext.GetCommand(IQueryable) Method
Parameters. query: IQueryable. The query whose SQL command information is to be retrieved. Returns.
Read more >
Getting the SQL query from an Entity Framework IQueryable
I am currently testing that an extension method I created works nicely with Entity Framework – all of the tests on a basic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found