[英]C# Update a List from Another List
I have 2 List<object>
. 我有2个
List<object>
。 The first one, lets call it ListA is more like a complete list and the second one ListB is a modified list. 第一个,让我们称之为ListA更像是一个完整的列表,第二个ListB是一个修改过的列表。 Now what I want to do is to modify ListA with ListB.
现在我想做的是用ListB修改ListA。 Is this doable and how can I do it.
这是可行的,我该怎么做。 This is what I have so far but doesn't work:
这是我到目前为止但不起作用:
var ListB = _repository.Get(m => m.Approved == true).ToList();
foreach (var x in ListB)
{
ListA.Where(d => d.Name == x.Name).First() = x;
}
return ListA;
EDIT: Visual Presentation to describe what 'modify' means in my situation 编辑:视觉演示,描述“修改”在我的情况下意味着什么
ListA
Id Name Age
1 John 14
2 Mark 15
3 Luke 13
4 Matthew 18
ListB
Id Name Age
2 Mark 0
4 Matthew 99
After 'modifying' it, ListA should look like: “修改”后,ListA应如下所示:
ListA
Id Name Age
1 John 14
2 Mark 0
3 Luke 13
4 Matthew 99
As I consider, you want to update only an age. 我认为,您只想更新一个年龄。 Also you don't need to use
Where().First()
you can use just First()
. 此外,您不需要使用
Where().First()
您可以只使用First()
。
foreach (var x in ListB)
{
var itemToChange = ListA.First(d => d.Name == x.Name).Age = x.Age;
}
If you are not sure, that item exists in ListA
you should use FirstOrDefault()
and if statement to check it. 如果您不确定,
ListA
存在ListA
您应该使用FirstOrDefault()
和if语句来检查它。
foreach (var x in ListB)
{
var itemToChange = ListA.FirstOrDefault(d => d.Name == x.Name);
if (itemToChange != null)
itemToChange.Age = x.Age;
}
Where and First return IEnumerable - you can modify only node of the list, but not reassign. Where和First返回IEnumerable - 您只能修改列表的节点,但不能重新分配。
using System.Collections.Generic;
//...
var itemToUpdate = ListA.FirstOrDefault(d => d.Name == x.Name);
if (itemToUpdate != null) {
ListA[ListA.IndexOf(itemToUpdate)] = x;
}
ListA.First(d => d.Name == x.Name).Update(x);
to elaborate aershov's answer: 详细说明aershov的答案:
ListA.Where(d => d.Name == x.Name).First().CopyFrom(x);
then in your Person class: 然后在你的Person类中:
public class Person
{
// ... Name, Id, Age properties...
public void CopyFrom(Person p)
{
this.Name = p.Name;
this.Id = p.Id;
this.Age = p.Age;
}
}
of course check nulls and everything. 当然检查空值和一切。
You could remove all elements of ListB
from ListA
based on Id
, add ListB to ListA
and then sort using Id
. 您可以根据
Id
从ListA
删除ListB
所有元素,将ListB添加到ListA
,然后使用Id
进行排序。
var newlist = ListA.Where(s => !ListB.Any(p => p.Id == s.Id)).ToList();
newlist.AddRange(ListB);
ListA = newlist.OrderBy(o => o.Id).ToList();
You could also use the Union method with an IEqualityComparer: 您还可以将Union方法与IEqualityComparer一起使用:
var newList = ListB.Union(ListA, new PersonEqualityComparer());
class PersonEqualityComparer : IEqualityComparer<Person>
{
public bool Equals(Person person1, Person person2)
{
if (person1 == null && person2 == null)
return true;
else if ((person1 != null && person2 == null) ||
(person1 == null && person2 != null))
return false;
return person1.Id.Equals(person2.Id);
}
public int GetHashCode(Person item)
{
return item.Id.GetHashCode();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.