Entity Framework Dynamic Lambda to Perform Search

2019-06-09 09:25发布

I have the following entities in Entity Framwork 5 (C#):

OrderLine - Id, OrderId, ProductName, Price, Deleted

Order - Id, CustomerId, OrderNo, Date

Customer - Id, CustomerName

On the order search screen the user can enter the following search values:

ProductName, OrderNo, CustomerName

For Example they might enter:

Product Search Field: 'Car van bike'

Order Search Field: '100 101 102'

Customer Search Field: 'Joe Jack James'

This should do a OR search (ideally using linq to entities) for each entered word, this example would output the following where sql.

(ProductName like 'Car' Or ProductName like 'van' Or ProductName like 'bike') AND

(OrderNo like '100' Or OrderNo like '101' Or OrderNo like '102') AND

(CustomerName like 'Joe' Or CustomerName like 'Jack' Or CustomerName like 'James')

I want to do this using linq to entities, i am guessing this would need to be some sort of dynamic lambda builder as we don't know how many words the user might enter into each field.

How would i go about doing this, i have had a quick browse but cant see anything simple.

4条回答
可以哭但决不认输i
2楼-- · 2019-06-09 10:08

There are 2 basic approaches to Dynamic Expressions and Queries in LINQ. 3 if you count using Json as the approach to get a lambda expression. => Akash Kava post

a) String Dynamic Lambda

System.Linq.Dynamic can be found at following links http://msdn.microsoft.com/en-US/vstudio/bb894665.aspx http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx http://www.scottgu.com/blogposts/dynquery/dynamiclinqcsharp.zip

b) Build Expression trees

More powerful but harder to master... Build expressions trees with code found here: http://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx

查看更多
你好瞎i
3楼-- · 2019-06-09 10:08

Disclaimer: I am author of Entity REST SDK.


You can look at Entity REST SDK at http://entityrestsdk.codeplex.com

You can query using JSON syntax as shown below,

 /app/entity/account/query?query={AccountID:2}&orderBy=AccountName
     &fields={AccountID:'',AcccountName:''}

You can use certain extensions provided to convert JSON to lambda.

And here is details of how JSON is translated to Linq. http://entityrestsdk.codeplex.com/wikipage?title=JSON%20Query%20Language&referringTitle=Home

Current Limitations of OData v3

Additionally, this JSON based query is not same as OData, OData does not yet support correct way to search using navigation properties. OData lets you search navigation property inside a selected entity for example Customer(1)/Addresses?filter=..

But here we support both Any and Parent Property Comparison as shown below.

Example, if you want to search for List of Customers who have purchased specific item, following will be query

 { 'Orders:Any': { 'Product.ProductID:==': 2 } }

This gets translated to

  Customers.Where( x=> x.Orders.Any( y=> y.Product.ProductID == 2))

There is no way to do this OData as of now.

Advantages of JSON

When you are using any JavaScript frameworks, creating query based on English syntax is little difficult, and composing query is difficult. But following method helps you in composing query easily as shown.

function query(name,phone,email){
   var q = {};
   if(name){
       q["Name:StartsWith"] = name;
   }
   if(phone){
       q["Phone:=="] = phone;
   }
   if(email){
       q["Email:=="] = email;
   }
   return JSON.stringify(q);
}

Above method will compose query and "AND" everything if specified. Creating composable query is great advantage with JSON based query syntax.

查看更多
Viruses.
4楼-- · 2019-06-09 10:20

You can build a lambda expression using Expression Trees . What you need to do is split the value and build the expression . Then you can convert in in to a lambda expression like this,

var lambda = Expression.Lambda<Func<object>>(expression);  

Here is an example

查看更多
放荡不羁爱自由
5楼-- · 2019-06-09 10:20

I would recomend to go slightly different way from answers above and use EntitySQL as it is trivial to build SQL-like string with dynamic conditions.

http://msdn.microsoft.com/en-us/library/bb738683.aspx

查看更多
登录 后发表回答