Convert -- a simple solution to the problem of multi-condition combination of LINQ

Source: Internet
Author: User

In this article, I use clear examples to solve the multi-condition problem of LINQ and have a clear idea. I also carefully described the problem and hoped to help you.

Recently, I have a project preparation function revision. My brother told me to do everything possible to achieve all kinds of LINQ, so a lot of things should be moved from the storage process .. when I wrote the evaluation function yesterday, I encountered a problem of multi-conditional combination of LINQ. The requirements are as follows:

Criteria for Evaluating multiple types of queries:

1. the query type of the Multi-condition of LINQ:

Rating received _ buyer's rating to me, rating received _ seller's rating to me, rating given _ buyer's rating, rating given _ seller's rating

  1. Public EnumOpinionsearchtype
  2. {
  3. Rating received _ buyer's rating = 0,
  4. Ratings received by sellers = 1,
  5. Comments provided _ my comments to buyers = 2,
  6. Comments given _ my comments to sellers = 3
  7. }

2. Evaluate the multi-criteria of LINQ:

All, praise, medium rating, negative rating

  1. Public EnumOpiniontype
  2. {
  3. All = 0,
  4. Praise = 1,
  5. Medium rating = 2,
  6. Negative rating = 3
  7. }

3. Time for evaluating and querying the multi-condition of LINQ:

All, within a week, within a month, within six months, six months away

  1. Public EnumOpiniontime
  2. {
  3. All = 0,
  4. = 1 in a week,
  5. Less than one month = 2,
  6. Less than six months = 3,
  7. 6 months away = 4
  8. }

For caching purposes, you need to upload the expression to the interface to obtain the corresponding list <comment>,

There are 3 conditions in total and 13 sub-conditions. After the combination is arranged, there will be 80 combinations.-If you really write them one by one, it's really tiring ..

The best way is to split all three conditions, complete different expressions, and combine the three conditions to form a new expression. the comparison found on the Internet is only the single-condition parameter. After checking msdn, we can see that there is an expression. and (left, right) can fulfill my needs. exploitation. the Extension Method of net3.5 is written as a combination expression method, and several multi-parameter expressions are reloaded, as follows:

  1. # Region expression
  2. Public StaticExpression contactexpressions (ThisExpression exp,ParamsExpression [] exps ){
  3. Foreach(VAR EInExps ){
  4. If(Null= E)Continue;
  5. Exp = expression. And (exp, e );
  6. }
  7. ReturnExp;
  8. }
  9. Public StaticExpression <func> contactexpressions (ThisExpression exp,ParamsExpression [] exps ){
  10. Foreach(VAR EInExps ){
  11. If(Null= E)Continue;
  12. Exp = expression. And (exp, e );
  13. }
  14. Return(Expression <func>) exp;
  15. }
  16.  
  17.  Public StaticExpression <func <t1, T> contactexpressions <t1, T> (ThisExpression exp,ParamsExpression [] exps ){
  18. Foreach(VAR EInExps ){
  19. If(Null= E)Continue;
  20. Exp = expression. And (exp, e );
  21. }
  22. Return(Expression <func <t1, T>) exp;
  23. }
  24.  Public StaticExpression <func <T1, T2, T> contactexpressions <T1, T2, T> (ThisExpression exp,ParamsExpression [] exps ){
  25. Foreach(VAR EInExps ){
  26. If(Null= E)Continue;
  27. Exp = expression. And (exp, e );
  28. }
  29. Return(Expression <func <T1, T2, T>) exp;
  30. }
  31.  Public StaticExpression <func <T1, T2, T3, T> contactexpressions <T1, T2, T3, T> (ThisExpression exp,
  32.  ParamsExpression [] exps ){
  33. Foreach(VAR EInExps ){
  34. If(Null= E)Continue;
  35. Exp = expression. And (exp, e );
  36. }
  37. Return(Expression <func <T1, T2, T3, T>) exp;
  38. }
  39. # Endregion

With these methods for multi-condition query of LINQ, the original requirements can be solved:

  1. Expression <func <split_opinion, <span = "">Bool> Exshortarchtype =Null;
  2. Expression <func <split_opinion, <span = "">Bool> Expopiniontype =Null;
  3. Expression <func <split_opinion, <span = "">Bool> Expopiniontime =Null;
  4. Switch(Searchtype ){
  5. CaseOpinionsearchtype. Comments _ my comments to buyers:
  6. Exshortarchtype = y => Y. userid = userid &&! Y. isseller;
  7. Break;
  8. CaseOpinionsearchtype. Comments _ my comments to sellers:
  9. Exshortarchtype = y => Y. userid = userid & Y. isseller;
  10. Break;
  11. CaseOpinionsearchtype. Comments received _ Comments from buyers:
  12. Exshortarchtype = y => Y. touserid = userid &&! Y. isseller;
  13. Break;
  14. CaseOpinionsearchtype. Comments received _ Comments the seller gave me:
  15. Exshortarchtype = y => Y. touserid = userid &&! Y. isseller;
  16. Break;
  17. }
  18.  Switch(Opintype ){
  19. CaseOpiniontype. Praise:
  20. Expopiniontype = y => Y. opiniontype = 0;
  21. Break;
  22. CaseOpiniontype. medium rating:
  23. Expopiniontype = y => Y. opiniontype = 1;
  24. Break;
  25. CaseOpiniontype. Negative rating:
  26. Expopiniontype = y => Y. opiniontype = 2;
  27. Break;
  28. }
  29.  Switch(Opintime ){
  30. CaseOpiniontime. Within a week:
  31. Expopiniontime = y => (datetime. Now-y. opiniontime). Days <= 7;
  32. Break;
  33. CaseOpiniontime. Within one month:
  34. Expopiniontime = y => (datetime. Now-y. opiniontime). Days <= 30;
  35. Break;
  36. CaseOpiniontime. Within six months:
  37. Expopiniontime = y => (datetime. Now-y. opiniontime). Days <= 180;
  38. Break;
  39. CaseOpiniontime. Six months later:
  40. Expopiniontime = y => (datetime. Now-y. opiniontime). days> 180;
  41. Break;
  42. }
  43. // Getpaged (Params) is used to obtain the list and support cache saving.
  44.  ReturnGetpaged (exshortarchtype. contactexpressions <split_opinion, <span = "">Bool> (Expopiniontype, expopiniontime ),
  45. Userid. usertableprefx (),True, Pageindex, pagesize );

The above describes how to parse the alternative usage of LINQ through a LINQ instance and then solve the problem of multi-condition combination of LINQ.

Address: http://developer.51cto.com/art/200909/151931.htm

Convert -- a simple solution to the problem of multi-condition combination of LINQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.