您如何添加动态' where' linq查询的子句? [英] How do you add dynamic 'where' clauses to a linq query?

查看:82
本文介绍了您如何添加动态' where' linq查询的子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带位掩码的User表,其中包含用户的角色。下面的linq查询返回角色包括1、4或16的所有用户。

I've got a User table with a bitmask that contains the user's roles. The linq query below returns all the users whose roles include 1, 4 or 16.

var users = from u in dc.Users
            where ((u.UserRolesBitmask & 1) == 1)
               || ((u.UserRolesBitmask & 4) == 4)
               || ((u.UserRolesBitmask & 16) == 16)
            select u;

我想将其重写为以下方法,以返回给定角色的所有用户,因此我可以重复使用它:

I'd like to rewrite this into the method below to returns all the users from the given roles so I can reuse it:

private List<User> GetUsersFromRoles(uint[] UserRoles) {}

关于如何动态构建查询的任何指针?谢谢

Any pointers on how to dynamically build my query? Thanks

推荐答案

您可以使用 PredicateBuilder 类。

PredicateBuilder已在 LINQKit NuGet包

PredicateBuilder has been released in the LINQKit NuGet package


LINQKit是LINQ的一组免费扩展SQL和Entity Framework高级用户。

LINQKit is a free set of extensions for LINQ to SQL and Entity Framework power users.

这篇关于您如何添加动态&amp;#39; where&amp;#39; linq查询的子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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