动态构建 Func 列表- 然后应用于 linq 查询

发布于 2024-12-02 10:21:54 字数 764 浏览 0 评论 0 原文

不确定这是否是最好的方法,但是这是我的想法

我正在使用实体框架数据模型 v 4.1 - 我正在尝试动态构建一个 where 语句,这样我就不必每次都查询数据库,而是可以构建一个“列表”条件,然后一次应用它们,这样数据库只被查询一次,而不是每次我添加新的条件 - 如果这有意义......

这是我所拥有的,

List<Func<Order, bool>> orderFunctions = new List<Func<Order, bool>>();
if (this.LoggedInUser.UserId == 9)
{
Func<Order, bool> deleg = o => o.Status.Equals(OrderStatus.NewOrder);
orderFunctions.Add(deleg);
}
else if (this.LoggedInUser.UserId == 22)
{
Func<Order, bool> deleg = o => o.AssignedToUserId.Equals(22);
orderFunctions.Add(deleg);
}
List<Orders> orders = Entities.Orders.Where( Some How Apply my Order Functions List Here ).ToList();

我不确定我是否即使在这里采取正确的方法 -希望这是有道理的 - 任何指导,示例代码都会很棒,我花了很多时间在网上寻找示例/教程

Not sure if this is the best approach, however here are my thoughts

I am using Entity Framework Data Model v 4.1 - i'm trying to build a where statement dynamically so that I dont have to query the db everytime, instead i can build a "list of" conditionals, then apply them all at once so the db is only queryed once as opposed to everytime i add my new conditional - if that makes sense...

here is what i have

List<Func<Order, bool>> orderFunctions = new List<Func<Order, bool>>();
if (this.LoggedInUser.UserId == 9)
{
Func<Order, bool> deleg = o => o.Status.Equals(OrderStatus.NewOrder);
orderFunctions.Add(deleg);
}
else if (this.LoggedInUser.UserId == 22)
{
Func<Order, bool> deleg = o => o.AssignedToUserId.Equals(22);
orderFunctions.Add(deleg);
}
List<Orders> orders = Entities.Orders.Where( Some How Apply my Order Functions List Here ).ToList();

I'm not sure if i'm even taking the right approach here - hopefully this makes sense - any guidance, sample code would be fantastic, i've having a heck of a time finding examples / tutorials for this online

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-12-09 10:21:54

您只需多次调用 Where 即可完成此操作:

IQueryable<Order> query = Entities.Orders;

if (this.LoggedInUser.UserId == 9)
{
    query = query.Where(o => o.Status.Equals(OrderStatus.NewOrder));
}
else if (this.LoggedInUser.UserId == 22)
{
    query = query.Where(o => o.AssignedToUserId.Equals(22));
}

List<Order> orders = query.ToList();

在您开始尝试检索结果之前,它不会联系数据库。

如果您确实想要创建一个列表,则需要创建一个List>> - 它们必须是表达式树而不是委托,以便实体框架可以处理它们。然后你可以这样做:

foreach (var predicate in predicates)
{
    query = query.Where(predicate);
}

或者你可以使用 PredicateBuilder 来构建单个表达式树。

You can do this just by calling Where multiple times:

IQueryable<Order> query = Entities.Orders;

if (this.LoggedInUser.UserId == 9)
{
    query = query.Where(o => o.Status.Equals(OrderStatus.NewOrder));
}
else if (this.LoggedInUser.UserId == 22)
{
    query = query.Where(o => o.AssignedToUserId.Equals(22));
}

List<Order> orders = query.ToList();

Until you start trying to retrieve the results, it won't contact the database.

If you really want to create a list, you'd need to create a List<Expression<Func<Order, bool>>> - they have to be expression trees rather than delegates, so that the Entity Framework can deal with them. Then you could just do:

foreach (var predicate in predicates)
{
    query = query.Where(predicate);
}

Or you could use PredicateBuilder to build up a single expression tree.

-黛色若梦 2024-12-09 10:21:54

乔恩当然给出了如何做得更好的答案。

但对于你最初的观点,以及为什么这不起作用:

应用洞列表并不困难(只需执行

x => orderFunctions.All(f => f(x))

),但你不会从中得到任何 SQL-love - 这意味着你会得到一个错误,因为 EF 不知道什么与所有(或您将在那里编码的任何内容)有关。
您必须查询所有条目(使用 ToList 或 ToArray),之后它将起作用......但没有性能;)

Jon gave - of course - the answer how to do this better.

But for your original point, and why this won't work:

It is not dificult to apply the hole list (just do

x => orderFunctions.All(f => f(x))

) but you will not get any SQL-love from this - meaning you will get an error because EF cannot know what to do with the all (or whatever you will code there).
You would have to query all entries (with ToList, or ToArray) and after this it would work ... but without performance ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文