1

我正在尝试对未选择的元素进行过滤“位置”并且不在 ViewModel 中

这就是我拥有的:

var UserQuery = (from u in db.USER
                join c in db.CONSULT on u.IdUser equals c.IdUser
                select new UserSexViewModel {  IdUser= c.IdUser, UserSex=u.Sex}).Distinct();

我想稍后UserQuery通过属于对象 CONSULT 或对象 USER 的任何属性过滤变量

例如,稍后按以下方式过滤:

CONSULT.TypeConsul, 
CONSULT.DateCreation,
USER.Name,
USER.Age, and many more...

像这样的东西:

UserQuery.Where(q=>q.CONSULT.TypeConsul==2) or UserQuery.Where(q=>q.USER.Age>16)

我怎么能做到这一点?

4

2 回答 2

0

LINQ 动态查询库将为您提供帮助。

请参考 http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

于 2012-07-27T02:16:50.083 回答
0
var UserQuery = (from u in db.USER
                join c in db.CONSULT on u.IdUser equals c.IdUser
                select new UserSexViewModel {  IdUser= c.IdUser, UserSex=u.Sex}).Distinct();

//伪代码

var specification = PredicateBuilder.True<Consult>();//default: build from this
specification = PredicateBuilder.And(specification, x => x.TypeConsult == 2); 
var additionalCriterias = ...;
specification = additionalCriterias == null || additionalCriterias.Length == 0 
                  ? specification 
                  : GetComposedSpecification(additionalCriterias);

var users = UserQuery.Where(specification).ToList()

predicateBuilder @ http://www.albahari.com/nutshell/predicatebuilder.aspx还有更多示例

于 2012-07-27T06:51:07.710 回答