创建一个共同的谓词函数 [英] Creating a common predicate function

查看:93
本文介绍了创建一个共同的谓词函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不知道什么条件使用来问这个问题,这可能是为什么我还没有找到一个答案,从搜索自己。

Firstly, I am not sure what terms to use to ask this question, which is probably why I have not found an answer from searching myself.

所以我使用LINQ到SQL(C#.NET 4中)工作,我希望得到匹配条件的所有用户,其中我会做这样的事情基本的列表:

So I am working with Linq to SQL (C#, .Net 4) and I want to get a list of all users that match a criteria, the basics of which I would do something like this:

var users = DataContext.Users.Where(x => x.Criteria1 == "something");



但在这种情况下,有几个字段我想匹配,事情是这些特定的字段​​是一个常见的​​检查,我希望能够创建,我可以用在我的任何用户查询来检查本场比赛一个专功能。

but in this case there are a few fields I want to match, the thing is these particular fields are a common check and I would like to be able to create a dedicating function that I can use within any of my user queries to check for this match.

要试着解释一个好一点的可以举一个例子:比方说,用户有5面旗帜,我希望有一个共同的检查,看是否的任何的这些标志的设置。所以,我可以写我的查询,像这样:

To try and explain that a bit better lets give an example: Lets say a user has 5 flags, and I want a common check to see if any of those flags are set. So I could write my query like so:

var users = DataContext.Users.Where(x => x.Flag1 || x.Flag2 || x.Flag3 || x.Flag4 || x.Flag5);



但我想这样做是单独指出,5标志检查这样我就可以用它在其它查询过,最后我想用这样的:

But what I would like to do is seperate out that "5 flag check" so I can use it in other queries too, ultimately I would like to use something like:

var users = DataContext.Users.Where(x => x.Criteria1 == "something" && CheckForFlags(x));



我已经有这样的功能试过这样:

I have tried this by having a function like this:

static bool CheckForFlags(User user)
{
   return user.Flag1 || user.Flag2 || user.Flag3 || user.Flag4 || user.Flag5;
}



但我得到一个错误:

but I get an error:

方法'布尔CheckForFlags(用户)没有支持的翻译
SQL。

"Method 'Boolean CheckForFlags(User)' has no supported translation to SQL."

...这是有道理的,但它有什么我可以做,使这项工作,我希望它的方式吗?或者这是SQL的限制,因为我使用LINQ实际上是在东西会使用LINQ工作对象?

...which makes sense, but it there something I can do to make this work the way I want it to? Or is this a restriction because I am using Linq to SQL and is in fact something that would work with Linq to Objects?

推荐答案

有关的LINQ to SQL如何处理表达式整洁的事情是,你实际上可以在你的代码的其他地方打造出来的表情和在查询中引用它们。你为什么不尝试是这样的:

The neat thing about how LINQ to SQL handles expressions is that you can actually build out expressions elsewhere in your code and reference them in your queries. Why don't you try something like this:

public static class Predicates
{
    public static Expression<Func<User, bool>> CheckForFlags()
    {
        return (user => user.Flag1 || user.Flag2 || user.Flag3 ||
                        user.Flag4 || user.Flag5);
    }

    public static Expression<Func<User, bool>> CheckForCriteria(string value)
    {
        return (user => user.Criteria1 == value);
    }
}



一旦你有你的谓词定义的,它很容易在查询中使用它们。

Once you have your predicates defined, it's very easy to use them in a query.

var users = DataContext.Users
    .Where(Predicates.CheckForFlags())
    .Where(Predicates.CheckForCriteria("something"));

这篇关于创建一个共同的谓词函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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