Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Linq Multi-condition combination

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要为大家展示了"如何解决Linq多条件组合问题",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何解决Linq多条件组合问题"这篇文章吧。

多种查询评价的条件:

1.Linq多条件之查询类型:

收到的评价_买家给我的评价,收到的评价_卖家给我的评价,给出的评价_我给买家的评价,给出的评价_我给卖家的评价

public enum OpinionSearchType { 收到的评价_买家给我的评价 = 0, 收到的评价_卖家给我的评价 = 1, 给出的评价_我给买家的评价 = 2, 给出的评价_我给卖家的评价 = 3 }

2.Linq多条件之评价类型:

全部,好评,中评,差评

public enum OpinionType { 全部 = 0, 好评 = 1, 中评 = 2, 差评 = 3 }

3.Linq多条件之评价查询时间:

全部,一个星期内,一个月以内,六个月以内,六个月以外

public enum OpinionTime { 全部 = 0, 一个星期内 = 1, 一个月以内 = 2, 六个月以内 = 3, 六个月以外 = 4 }

由于缓存的需要,要把Expression完成之后再传到接口那边获取相应的List.按照这样的看的话,

总共3个条件, 13个子条件, 排列组合之后, 会有80种的组合. - - 真的一个个组合去写的话,还真是累死人了..

左思右想,***的方法就是把3个条件都拆开来,完成不同的Expression,到***再把三个条件组合在一起成为一个新的Expression.网上找到的比较都只是单条件的Parameter, 查了MSDN,才知道有个Expression.And(left, right)可以完成我的需求.利用.net3.5的扩展方法写成了一个组合Expression的方法,再重载了几个多参数的表达式,如下:

#region 表达式 public static Expression ContactExpressions(this Expression exp, params Expression[] exps) { foreach (var e in exps) { if (null == e) continue; exp = Expression.And(exp, e); } return exp; } public static Expression ContactExpressions(this Expression exp, params Expression[] exps) { foreach (var e in exps) { if (null == e) continue; exp = Expression.And(exp, e); } return (Expression)exp; } public static Expression ContactExpressions(this Expression exp, params Expression[] exps) { foreach (var e in exps) { if (null == e) continue; exp = Expression.And(exp, e); } return (Expression)exp; } public static Expression ContactExpressions(this Expression exp, params Expression[] exps) { foreach (var e in exps) { if (null == e) continue; exp = Expression.And(exp, e); } return (Expression)exp; } public static Expression ContactExpressions(this Expression exp, params Expression[] exps) { foreach (var e in exps) { if (null == e) continue; exp = Expression.And(exp, e); } return (Expression)exp; } #endregion

有了这几个方法进行Linq多条件查询,原本的需求就可以迎刃而解了:

Expression expSearchType = null; Expression expOpinionType = null; Expression expOpinionTime = null; switch (searchType) { case OpinionSearchType.给出的评价_我给买家的评价: expSearchType = Y => Y.UserID == userID && !Y.IsSeller; break; case OpinionSearchType.给出的评价_我给卖家的评价: expSearchType = Y => Y.UserID == userID && Y.IsSeller; break; case OpinionSearchType.收到的评价_买家给我的评价: expSearchType = Y => Y.ToUserID == userID && !Y.IsSeller; break; case OpinionSearchType.收到的评价_卖家给我的评价: expSearchType = Y => Y.ToUserID == userID && !Y.IsSeller; break; } switch (opinType) { case OpinionType.好评: expOpinionType = Y => Y.OpinionType == 0; break; case OpinionType.中评: expOpinionType = Y => Y.OpinionType == 1; break; case OpinionType.差评: expOpinionType = Y => Y.OpinionType == 2; break; } switch (opinTime) { case OpinionTime.一个星期内: expOpinionTime = Y => (DateTime.Now - Y.OpinionTime).Days (DateTime.Now - Y.OpinionTime).Days (DateTime.Now - Y.OpinionTime).Days (DateTime.Now - Y.OpinionTime).Days > 180; break; } //GetPaged(params) 这个方法是用来获取列表并支持缓存保存的. return GetPaged(expSearchType.ContactExpressions(expOpinionType, expOpinionTime), userID.UserTablePrefx(), true, pageIndex, pageSize);以上是"如何解决Linq多条件组合问题"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report