【问题标题】:Linq - Using array in Lambda expression to fetch multiple recordsLinq - 在 Lambda 表达式中使用数组来获取多条记录
【发布时间】:2011-09-18 17:53:03
【问题描述】:

我不确定这是否可能。我想创建一个包含一些简单 id 的数组(或列表/字典),并在 lambda 表达式中使用该数组(或其他)。

下面的例子应该返回 UserId 的 15850 和 15858

DbDataContext db = new DbDataContext();    
int[] userIds = {15850, 15858};
var users = db.tblUsers.Where(x => x.UserId.Equals(userIds));

有没有可能?

谢谢

【问题讨论】:

    标签: c# linq lambda


    【解决方案1】:

    这是可能的,并且将转换为 SQL WHERE IN (...) 语句,但它在 linq 中是倒着写的:

    DbDataContext db = new DbDataContext();    
    int[] userIds = {15850, 15858};
    var users = db.tblUsers.Where(x => userIds.Contains(x.UserId));
    

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 1970-01-01
      • 2017-05-31
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2012-09-10
      • 1970-01-01
      相关资源
      最近更新 更多