Example Analysis of LINQ to SQL dynamic query
This article introduces the relevant knowledge of "LINQ to SQL dynamic query example Analysis". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
LINQ to SQL dynamic query
Using LINQ to SQL dynamic queries, this example uses the CreateQuery () method to create a statement that outputs the query with an expression of type IQueryable.
Here's an example:
Var C1 = Expression.Parameter (typeof (Customer), "c"); PropertyInfo City = typeof (Customer) .GetProperty ("City"); var pred = Expression.Lambda (Expression.Equal (Expression.Property (C1, City), Expression.Constant ("Seattle"), C1); IQueryable custs = db.Customers; Expression expr = Expression.Call (typeof (Queryable), "Where", new Type [] {custs.ElementType}, custs.Expression, pred); IQueryable Q = db.Customers.AsQueryable (). Provider.CreateQuery (expr)
The Log property is used to print SQL queries or commands to TextReader. This approach can be useful for understanding LINQ to SQL functionality and debugging specific problems.
The following example uses the Log property to display the SQL code in the console window before it is executed. We can use this property with query, insert, update, and delete commands.
/ / turn off log function / / db.Log = null; / / use log function: log output to console window db.Log = Console.Out; var Q = from c in db.Customers where c.City = = "London" select c; / / log output to file StreamWriter sw = new StreamWriter ("log.txt"), true); db.Log = sw; var q = from c in db.Customers where c.City = = "London" select c Sw.Close (); "LINQ to SQL dynamic query example Analysis" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!