Get Where parameters from IQueryable
See original GitHub issueI’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:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
sdanyliv it’s INCREDIBLE, works as sharm !! a lot of thanks for the excelent support!!!
You can find me in skype svyatoslav.danyliv, i’m also from Ukraine