Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of Linq to sql dynamic query

Shulou Source: shulou.com Published: 2022-06-02 09:03:04 10月04日 Update

This article introduces the relevant knowledge of "what is the method of Linq to sql dynamic query". In the operation of actual cases, many people will encounter such a dilemma, so 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 is really a headache. You have to write a lot of code with expression tree or reflection method. Sometimes you might as well go back to the era of manual sql splicing instead of being so complicated, but the technology is always moving forward, and you still have to solve this problem on Linq. I have no intention of finding a relatively simple way to share it on the Internet:

Void ShowData () {using (DBDataContext db = new DBDataContext (Database.ConnectionString)) {Expression expr = n = > GetCondition (n); var _ query = db.T_Bargins.Where (expr.Compile ()) .Select (b = > new {b.F_Money, b.F_Name}) Foreach (var item in _ query) {Response.Write (item.F_Name + "" + item.F_Money.ToString () + ");}

The DynamicQuery routines of both VB and C # include a source implementation of an auxiliary library that allows you to express LINQ queries using extended methods that accept string parameters rather than class-safe language operators. You can copy the implementation of C# or VB's DynamicQuery library into your own project and use it to build Linq to sql dynamic queries based on user input where appropriate.

Linq to sql dynamic query implementation obtains the condition that the contract amount is greater than 6000, or the contract name contains the word "should".

Private bool GetCondition (T_Bargin _ Table) {bool _ result = false; decimal _ MinMoney = 6000; if (_ MinMoney > 0) {_ result | = _ Table.F_Money > = _ MinMoney;} string _ Name = "Jianghua"; if (! String.IsNullOrEmpty (_ Name)) {_ result | = _ Table.F_Name.Contains (_ Name) } return _ result;} "what is the method of Linq to sql dynamic query" is introduced here, thank you for 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!

Tags: Query dynamic method content contract more knowledge Category Auxiliary complex practical appropriate headache Learning success next things Code methods parameters names Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Microsoft Shulou Information macOS