c# - LINQ to SQL 按 List<int> 过滤

标签 c# .net linq linq-to-sql

如何仅当 UserId 在我的用户列表中时才从表中检索行?

下面的代码不起作用:/

List<int> selectedSourceUsers = ...
MyModelDataContext context = ...

e.Result = from u in context.Users
                   from id in selectedSourceUsers
                   where u.UserId == id
                   select u;

感谢帮助

最佳答案

试试这个:

int[] selectedUsersArray = selectedSourceUsers.ToArray();
e.Result = from u in context.Users
           where selectedUsersArray.Contains(u.UserId)
           select u;

(老实说,我原以为它也可以与 List<int> 一起使用,但改用数组可能会解决问题...)

关于c# - LINQ to SQL 按 List<int> 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3555511/

相关文章:

.net - RegEx判断字符串是否不包含特定字符

c# - 优化还是放弃 LINQ 查询?

c# - 使用 Linq 执行 List.Exist

c# - 保存和读取 .txt 文件的特定部分

c# - 如何为 AES 生成持久 key

c# - List<int> 上的 AllConsecutives

c# - 使用 C# 更新另一台机器(共享文件夹/网络)上存在的文件(web.config)中的值

c# - 我是否需要重写我的异步方法/类来实现我想要的功能?

c# - 在两个日期时间之间获取随机 DateTime 的最佳做法是什么?

c# - 在 asp.net c# 中对 gridview 的列进行排序