选择基于ID与LINQ的列表多个记录 [英] Select multiple records based on list of Id's with linq
本文介绍了选择基于ID与LINQ的列表多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含我的用户配置
表标识的列表。我如何可以选择所有的基于身份的名单我在 VAR上了车
使用的UserProfiles
LINQ
?
I have a list containing Id's of my UserProfile
table. How can i select all UserProfiles
based on the list of Id's i got in a var
using LINQ
?
var idList = new int[1, 2, 3, 4, 5];
var userProfiles = _dataContext.UserProfile.Where(......);
我被困在这里。我能做到这一点使用循环等,但我宁愿做与 LINQ
。
推荐答案
您可以使用 包含()
了点。它会觉得有点倒退,当你真的想产生一个在
条款,但这应该这样做:
You can use Contains()
for that. It will feel a little backwards when you're really trying to produce an IN
clause, but this should do it:
var userProfiles = _dataContext.UserProfile
.Where(t => idList.Contains(t.Id));
我也假设每个用户配置
记录将有一个 INT
ID
字段。如果不是这种情况,你就必须进行相应的调整。
I'm also assuming that each UserProfile
record is going to have an int
Id
field. If that's not the case you'll have to adjust accordingly.
这篇关于选择基于ID与LINQ的列表多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文