简体   繁体   English

更新一个通用列表<t>与另一个通过 Linq c#</t>

[英]Updating one Generic List<T> with Another Via Linq c#

I'm able to achieve this in a for-each loop.我能够在 for-each 循环中实现这一点。 I'd like to achieve this via linq query to be as concise as possible.我想通过 linq 查询尽可能简洁地实现这一点。 Here is my foreach sample:这是我的 foreach 示例:

 foreach (var item in List2)
 {
     List1.Where(m => m.id == item.id).FirstOrDefault().Selected = item.Selected;
 }

So essentially, I'd like to return an updated list (List2)所以本质上,我想返回一个更新的列表(List2)

You are copying the Selected properties of the items from one list to another.您正在将项目的Selected属性从一个列表复制到另一个列表。 I think your foreach loop is too complicated, a cleaner foreach should be like this:我认为你的 foreach 循环太复杂了,一个更干净的 foreach 应该是这样的:

foreach (var item in List1)
{
    item.Selected = List2.First(m => m.id == item.id).Selected;
}

Although you may write one-liner using List.ForEach like below, but I prefer using foreach .尽管您可以像下面一样使用List.ForEach编写单行代码,但我更喜欢使用foreach

List1.ForEach(item => item.Selected = List2.First(m => m.id == item.id).Selected);

I prefer to do things like that by joining the lists.我更喜欢通过加入列表来做类似的事情。

var joined = from i1 in list1
             join i2 in list2 on i1.id = i2.id
             select new { i1, i2 };
joined.ToList().Foreach(x => x.i1.Selected = x.i2.Selected);

This is probably the most succinct way to do it.这可能是最简洁的方法。 A foreach loop will perform better though.不过, foreach循环的性能会更好。 It also prevents null reference exceptions, or ways to deal with them, because it automatically skips items that have no counterpart in list2 .它还可以防止 null 引用异常或处理它们的方法,因为它会自动跳过list2中没有对应项的项目。

Note that this works because list1 is an in-memory list, which I infer from your example.请注意,这是有效的,因为list1是一个内存列表,我从您的示例中推断出来。 If list1 is an IEnumerable it will be repopulated when it's accessed later on in the code, erasing the previous changes.如果list1是一个IEnumerable ,它将在稍后在代码中访问时重新填充,从而删除以前的更改。

You can do this with a combination of Where() and ForEach() method.您可以结合使用Where()ForEach()方法来做到这一点。

List1.Where(x => List2.Where(y=> y.id == x.id).ToList().Count > 0).ToList().ForEach(x => x.Selected = List2.FirstOrDefault(y => x.id == y.id).Selected)

and if you don't want to use ForEach() , you can use this with Select() :如果您不想使用ForEach() ,可以将其与Select()一起使用:

List1 = List1.Where(x => List2.Where(y=> y.id == x.id).ToList().Count > 0).ToList().Select(x => {x.Value = List2.FirstOrDefault(y => x.id == y.id).Value; return x;}).Concat(List1.Where(x => List2.Where(y=> y.id == x.id).ToList().Count == 0)).ToList();

However, it's too long in my opinion但是,我认为它太长了

Update: I fixed the problems Thanks to @canton7更新:感谢@canton7,我解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# LINQ 返回泛型列表<T> - C# LINQ Return Generic List<T> 通过 C# LINQ 访问通用列表中数组属性的元素 - Access the element of array property in a generic list via C# LINQ linq到通用列表C# - linq to generic list C# 在 C# 中使用 Linq 更新现有列表 - Updating an existing List using Linq in C# C#:如何比较两个集合(System.Collection.Generic.List <T> )使用Linq / Lambda? - C# : How to compare two collections (System.Collection.Generic.List<T>) using Linq/Lambda? C#LINQ如何从另一个列表中的一个列表中找到匹配项,并在另一个列表中检查属性 - C# LINQ How to find matching item from one list in another list and check for the property in the other list 如何将一个类属性的值设置为 C# 中另一个泛型列表中的另一个类属性? - How to set a value of one class property to another class property in another generic list in C#? C#LINQ组和总和列表 <T> 然后分组并求和第一个列表中的另一个列表 - C# LINQ Group and sum List<T> then Group and sum another list within the first List 无法将一个通用列表转换为另一个 - Can't convert one generic list to another 在通用列表中,是否可以通过声明式/ LINQ方式将一个属性复制到另一个属性? - In a generic list, is there a way to copy one property to another in a declarative /LINQ manner?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM