In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use LINQ fuzzy query". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use LINQ fuzzy query.
The multi-conditional compound search effect implemented by LINQ fuzzy query is as follows:
LINQ fuzzy query implementation phase 1:
Dynamic query can be carried out by using Lambda expression tree. Write a method for compound query, dynamically combine conditions, and generate Lambda expressions.
/ this method with paging function, through the input key value to NVC compound query / List GetPagedObjectsByNVC (int startIndex, int pageSize, NameValueCollection nvc, bool isAnd) {IQueryable query = Consulting.Instance.UserT_TractInfo; query.Where (t = > t.IsDel = = 0). Where (t = > t.IsAuditing = = 1); / / auditing and logical deletion Expression condition = null; ParameterExpression param = Expression. Parameter (typeof (UserT_TractInfo), "c"); int propertyCount = 0; foreach (string key in nvc) {Expression right = Expression.Constant (NVC [key]); / / key string keyProperty = key / property if (typeof (UserT_TractInfo) .GetProperty (keyProperty)! = null) / / when this property exists in the object (because there may be many other parameters for the key-value pair, such as page) {Expression left = Expression.Property (param, typeof (UserT_TractInfo) .GetProperty (keyProperty)); / / create the property Expression filter = Expression.Equal (left, right); / / filter if (condition = = null) {condition = filter } else {if (isAnd) {condition = Expression.And (condition, filter);} else {condition = Expression.Or (condition, filter);}} propertyCount++;}} / / above foreach combines the corresponding conditionExpression of each valid key-value pair. / / the most important combination of the composite query is done. If (propertyCount > 0) {Expression pred = Expression.Lambda (condition, param) MethodCallExpression whereCallExpression = Expression.Call (typeof (Queryable), "Where", new Type [] {typeof (UserT_TractInfo)}, Expression.Constant (query), pred); return Consulting.Instance.UserT_TractInfo.AsQueryable (). Provider.CreateQuery (whereCallExpression). OrderByDescending (t = > t.ID). Skip (startIndex-1). Take (pageSize). ToList (); / / query the result} else {return Consulting.Instance.UserT_TractInfo. OrderByDescending (t = > t.ID). Skip (startIndex-1). Take (pageSize) .ToList (); / / if there is no valid key-value pair, return all results}}
After working for a long time, I was very excited, but then I knew that the Lambda expression could not be written, and my heart was cold.
LINQ fuzzy query implementation phase 2:
Although Li Yongjing's article didn't help me much, there was a valuable reply behind it: "use the System.Linq.Dynamic provided by Microsoft to make it convenient." Soon found the corresponding examples and Dynamic.cs, but also found the "Linq to SQL Dynamic dynamic query", there are more detailed examples, but Dynamic.cs can not use like, hate ah!
Return Consulting.Instance.UserT_TractInfo.Where ("b_number = = @ 0", "P (2007) 031") .Orderby Descending (t = > t.ID). Skip (startIndex-1) .Take (pageSize) .ToList ()
The code is easy, but useless: (
The third stage of LINQ fuzzy query implementation:
Here release the core code, it is easy to understand, simple is beautiful!
SearchPredicate = PredicateExtensions. True (); foreach (string key in nvcParam) {string condition = string.Empty; switch (key) {case "b_number": condition = nvcParam [key]; searchPredicate = searchPredicate.And (u = > u.B_number.Contains (condition)); break; case "b_address": condition = nvcParam [key]; searchPredicate = searchPredicate.And (u = > u.B_address.Contains (condition)); break; case "b_canton": condition = nvcParam [key] SearchPredicate = searchPredicate.And (u = > u.B_canton.Contains (condition)); break; case "a_status": condition = nvcParam [key]; searchPredicate = searchPredicate.And (u = > u.A_status.ToString (). Contains (condition)); break; case "b_area": condition = nvcParam [key]; searchPredicate = searchPredicate.And (u = > u.B_area.Contains (condition)); break; case "c_clinchdate": condition = nvcParam [key] SearchPredicate = searchPredicate.And (u = > u.C_clinchdate.Contains (condition)); break; default: break;}} return Consulting.Instance.UserT_TractInfo. Where (searchPredicate). Orderby Descending (t = > t.ID). Skip (startIndex-1) .Take (pageSize) .ToList ()
The following is the PredicateExtensions after I wrote the comments. I don't know exactly how the True and False of the constructor work, but the result is just like my comments, which is very important when writing conditions for a composite query (but at present, I have completed the composite query by writing all AND, and I haven't done a lot of keyword OR yet):
/ when the constructor uses True: a single AND is valid, multiple AND is valid; / / a single OR is invalid, multiple OR is invalid; when mixing, the OR written after AND is valid / when the constructor uses False: a single AND is invalid, multiple AND is invalid; / / a single OR is valid, multiple OR is valid AND valid / public static class PredicateExtensions written after OR during mixing {return f = > true;} public static Expression False () {return f = > false;} public static Expression Or (this Expression expression1, Expression expression2) {var invokedExpression = Expression.Invoke (expression2, expression1.Parameters.Cast ()); return Expression.Lambda (Expression.Or (expression1.Body, invokedExpression), expression1.Parameters) } public static Expression And (this Expression expression1, Expression expression2) {var invokedExpression = Expression.Invoke (expression2, expression1.Parameters.Cast ()); return Expression.Lambda (Expression.And (expression1.Body, invokedExpression), expression1.Parameters);}} so far, I believe you have a better understanding of "how to use LINQ fuzzy query". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.