根据用户输入如何动态地建立并返回一个LINQ predicate [英] How to dynamically build and return a linq predicate based on user input

查看:83
本文介绍了根据用户输入如何动态地建立并返回一个LINQ predicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

变得有点粘在此。基本上我有,我想返回predicate EX pression,我可以作为一个WHERE条件使用的方法。 我想我需要做的是与此类似: http://msdn.microsoft .COM / EN-US /库/ bb882637.aspx ,但我有点坚持以什么我需要做的。

Getting a bit stuck on this. Basically I have a method that I want to return a predicate expression that I can use as a Where condition. I think what I need to do is similar to this: http://msdn.microsoft.com/en-us/library/bb882637.aspx but I'm a bit stuck as to what I need to do.

方法:

private static Expression<Func<Conference, bool>> GetSearchPredicate(string keyword, int? venueId, string month, int year)
{
    if (!String.IsNullOrEmpty(keyword))
    {
        // Want the equivilent of .Where(x => (x.Title.Contains(keyword) || x.Description.Contains(keyword)));
    }
    if (venueId.HasValue) 
    {
        // Some other predicate added...
    }

    return ??

}

实例应用:

var predicate = GetSearchPreducate(a,b,c,d);
var x = Conferences.All().Where(predicate);

我需要这种分离,这样我可以通过我的predicate到我的仓库,并用它在其他地方。

I need this separation so that I can pass my predicate into my repository and use it in other places.

推荐答案

你检查出 predicateBuilder

这篇关于根据用户输入如何动态地建立并返回一个LINQ predicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆