创建通用谓词函数

时间:2023-02-16
本文介绍了创建通用谓词函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不知道用什么术语来问这个问题,这可能是我自己搜索没有找到答案的原因.

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 to 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;
}

但我收到一个错误:

"方法 'Boolean CheckForFlags(User)' 不支持转换为SQL."

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

...这是有道理的,但我可以做些什么来使这项工作按照我想要的方式工作?或者这是一个限制,因为我使用的是 Linq to SQL 而实际上它可以与 Linq to Objects 一起使用?

...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"));

这篇关于创建通用谓词函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何从 LINQ DataContext.SubmitChanges() 获取 TSQL 查询 下一篇:SQLite.net SQLiteFunction 在 Linq to SQL 中不起作用

相关文章

最新文章