我正在使用 self 跟踪实体模型。 ProductInstallation
是一个 DTO,其中包含有关公司产品安装的所有详细信息。
UserRoles
实体持有产品-系统角色-用户 ID 之间的关系。
举个例子:
Product: Inventory
System Role : PurchasingUser
User ID : hasithaH <- (Suppose me)
使用下面的 LINQ 查询,我可以获得不同的 UserID。
string[] userIDs = productInstallation.UserRoles
.Select(u=>u.UserID).Distinct().ToArray();
现在我需要获取我在上述步骤中查询的 UserID 的所有用户配置文件。
productInstallation.SystemUsers = context.SystemUsers.Select(u=> u.UserID ..???
从 SQL 的角度来看,这是我想要的查询:
Select * from SystemUsers where UserID in ('UserA','UserB','UserC')
我应该如何编写 LINQ 查询来完成这项工作?
最佳答案
你这样写:
var result = context.SystemUsers.Where(su =>
productInstallation.UserRoles.Any(ur => su.UserID == ur.UserId));
或者如果两个源不是来自同一个数据库的 IQuerable
:
string[] userIDs = productInstallation.UserRoles
.Select(u=>u.UserID).Distinct().ToArray();
var result = context.SystemUsers.Where(su =>
userIDs.Contains(su.UserID));
关于c# - 如何编写 LINQ 查询以从具有给定匹配 ID 集的集合中进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17152489/