【问题标题】:Comparing Integer Array in Linq Query在 Linq 查询中比较整数数组
【发布时间】:2018-07-04 07:31:26
【问题描述】:

我目前正在构建的模块的要求之一是我需要使用 Linq 选择数据,但 ID 是通过整数数组传递给我的

        var CheckedArray = Request["doc_download"];
        string[] detailIds = CheckedArray.Split(',');
        List<int> dtIds = new List<int>();
        foreach (string words in detailIds)
        {
            dtIds.Add(Int32.Parse(words));
        }


      using (var ctx = new Connect2020Entities())
        {
           DetailList = (from userDetail in ctx.DocumentDetail
           where userDetail.ID == <!-- items in dtIds --> //<<I do not know what could be used to compare all of the data from the array in a single query>>

           select new DetailList()
           {
             FilePath = userDetail.FilePath
           }).ToList<UserDocument>();
        }

可以做些什么,以便可以在查询中一次性比较整数数组中的所有 id。我目前想不出一个可行的逻辑来允许将数组中的值用作我正在使用的查询中的参数。

*DocumentDetail字段ID是一个整数。

【问题讨论】:

    标签: asp.net arrays linq


    【解决方案1】:

    你可以尝试使用Contains()

    ctx.DocumentDetail.Where(ele => dtIds.Contains(ele.ID)).ToList();
    

    或者在你当前的语法中

    where dtIds.Contains(userDetail.ID)
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多