【问题标题】:Insert values in col1 of list 1 from col 2 of list 2 based on a common column根据公共列从列表 2 的 col 2 中插入值到列表 1 的 col1
【发布时间】:2020-06-09 01:25:23
【问题描述】:

清单 1:

|      User Id        |    Latest        |
+---------------------+------------------+
|          1          |         1        |
|          2          |         3        |
|          3          |         3        |
|          4          |         0        |

清单 2:

|      User Id        |    Latest        |        Rating    |
+---------------------+------------------+------------------+
|          1          |       null       |       10         |
|          2          |       null       |       12         |
|          3          |       null       |       11         |
|          4          |       null       |       16         |

我想根据加入/比较两个列表中User Id 列的值,将List1 的Latest 列的值插入List2 的Latest 列。

我可以使用 foreach 循环,但我猜它会运行 n*m 并且看起来很难看。有没有办法使用 LINQ 或有效地做到这一点?

问候。 朱奈德

【问题讨论】:

  • 到目前为止你尝试过什么?您为什么不通过dotnetfiddle.net 向我们提供您的样本数据以及相应的结果?
  • 你想将 list2 保存到数据库中吗?
  • 请勾选正确答案????

标签: c# asp.net list linq join


【解决方案1】:

试试这样的。这可能会解决您将最新值从 List1 添加到 List2 的问题。

List2.AddRange(List1.Select(user => new List1{
     Latest = user.Latest, 
     UserID = user.UserID
   }));

【讨论】:

    【解决方案2】:

    LINQ 永远不会更改任何源序列,它只能从中提取数据。

    您必须枚举提取的数据以更新原始表。

    var recordsToUpdate = List2.Join(List1,    // join List2 and List1
        list2Row => list2Row.UserId,           // from every row in List2 take UserId
        list1Row => list1Row.UserId,           // from every row in List1 take UserId
        (list2Row, list1Row) => new            // when they match make one new object
        {
            Id = list2Row.UserId,              // take UserId from list2
            Latest = list1Row.Latest,          // take Latest from list1
            Rating = list2Row.Rating,          // take Rating from list2
        })
        .ToList();                             // execute the query
    

    我不知道您如何更新记录。实体框架? SQL?会是这样的:

    foreach (var recordToUpdate in recordsToUpdate)
    {
         UpdateRecord(recordToUpdate.UserId, recordToUpdate.Latest, recordToUpdate.Rating)
         // TODO: implement this function
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用LINQ 来做到这一点:

      试试这个代码:

      List2.ForEach(item1 => 
      { 
          item1.Latest = List1.FirstOrDefault(item2 => item2.UserId == item1.UserId)?.Latest; 
      });
      

      请注意,Latest 必须是 Nullable。

      【讨论】:

        【解决方案4】:
        var result = from i1 in List1
                     join i2 in List2
                     on i1.UserId equals i2.UserId
                select new
                {
                    i2.UserId,
                    i1.Latest,
                    i2.Rating
                };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-07-06
          • 1970-01-01
          • 2022-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多