你如何添加动态',其中'条款,LINQ查询? [英] How do you add dynamic 'where' clauses to a linq query?

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

问题描述

我有使用包含用户的角色信息的位掩码的用户表。下面的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 to SQL和Entity Framework的电力用户。

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

这篇关于你如何添加动态',其中'条款,LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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