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
- Public EnumOpinionsearchtype
- {
- Rating received _ buyer's rating = 0,
- Ratings received by sellers = 1,
- Comments provided _ my comments to buyers = 2,
- Comments given _ my comments to sellers = 3
- }
2. Evaluate the multi-criteria of LINQ:
All, praise, medium rating, negative rating
- Public EnumOpiniontype
- {
- All = 0,
- Praise = 1,
- Medium rating = 2,
- Negative rating = 3
- }
3. Time for evaluating and querying the multi-condition of LINQ:
All, within a week, within a month, within six months, six months away
- Public EnumOpiniontime
- {
- All = 0,
- = 1 in a week,
- Less than one month = 2,
- Less than six months = 3,
- 6 months away = 4
- }
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:
- # Region expression
- Public StaticExpression contactexpressions (ThisExpression exp,ParamsExpression [] exps ){
- Foreach(VAR EInExps ){
- If(Null= E)Continue;
- Exp = expression. And (exp, e );
- }
- ReturnExp;
- }
- Public StaticExpression <func> contactexpressions (ThisExpression exp,ParamsExpression [] exps ){
- Foreach(VAR EInExps ){
- If(Null= E)Continue;
- Exp = expression. And (exp, e );
- }
- Return(Expression <func>) exp;
- }
-
- Public StaticExpression <func <t1, T> contactexpressions <t1, T> (ThisExpression exp,ParamsExpression [] exps ){
- Foreach(VAR EInExps ){
- If(Null= E)Continue;
- Exp = expression. And (exp, e );
- }
- Return(Expression <func <t1, T>) exp;
- }
- Public StaticExpression <func <T1, T2, T> contactexpressions <T1, T2, T> (ThisExpression exp,ParamsExpression [] exps ){
- Foreach(VAR EInExps ){
- If(Null= E)Continue;
- Exp = expression. And (exp, e );
- }
- Return(Expression <func <T1, T2, T>) exp;
- }
- Public StaticExpression <func <T1, T2, T3, T> contactexpressions <T1, T2, T3, T> (ThisExpression exp,
- ParamsExpression [] exps ){
- Foreach(VAR EInExps ){
- If(Null= E)Continue;
- Exp = expression. And (exp, e );
- }
- Return(Expression <func <T1, T2, T3, T>) exp;
- }
- # Endregion
With these methods for multi-condition query of LINQ, the original requirements can be solved:
- Expression <func <split_opinion, <span = "">Bool> Exshortarchtype =Null;
- Expression <func <split_opinion, <span = "">Bool> Expopiniontype =Null;
- Expression <func <split_opinion, <span = "">Bool> Expopiniontime =Null;
- Switch(Searchtype ){
- CaseOpinionsearchtype. Comments _ my comments to buyers:
- Exshortarchtype = y => Y. userid = userid &&! Y. isseller;
- Break;
- CaseOpinionsearchtype. Comments _ my comments to sellers:
- Exshortarchtype = y => Y. userid = userid & Y. isseller;
- Break;
- CaseOpinionsearchtype. Comments received _ Comments from buyers:
- Exshortarchtype = y => Y. touserid = userid &&! Y. isseller;
- Break;
- CaseOpinionsearchtype. Comments received _ Comments the seller gave me:
- Exshortarchtype = y => Y. touserid = userid &&! Y. isseller;
- Break;
- }
- Switch(Opintype ){
- CaseOpiniontype. Praise:
- Expopiniontype = y => Y. opiniontype = 0;
- Break;
- CaseOpiniontype. medium rating:
- Expopiniontype = y => Y. opiniontype = 1;
- Break;
- CaseOpiniontype. Negative rating:
- Expopiniontype = y => Y. opiniontype = 2;
- Break;
- }
- Switch(Opintime ){
- CaseOpiniontime. Within a week:
- Expopiniontime = y => (datetime. Now-y. opiniontime). Days <= 7;
- Break;
- CaseOpiniontime. Within one month:
- Expopiniontime = y => (datetime. Now-y. opiniontime). Days <= 30;
- Break;
- CaseOpiniontime. Within six months:
- Expopiniontime = y => (datetime. Now-y. opiniontime). Days <= 180;
- Break;
- CaseOpiniontime. Six months later:
- Expopiniontime = y => (datetime. Now-y. opiniontime). days> 180;
- Break;
- }
- // Getpaged (Params) is used to obtain the list and support cache saving.
- ReturnGetpaged (exshortarchtype. contactexpressions <split_opinion, <span = "">Bool> (Expopiniontype, expopiniontime ),
- 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