C# 使用 LINQ 将一个列表中的值分配给另一个列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9708266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign values from one list to another using LINQ
提问by user1269810
Hello I have a little problem with assigning property values from one lists items to anothers. I know i could solve it "the old way" by iterating through both lists etc. but I am looking for more elegant solution using LINQ.
您好,我在将一个列表项中的属性值分配给另一个列表项时遇到了一个小问题。我知道我可以通过遍历两个列表等来“以旧方式”解决它,但我正在寻找使用 LINQ 的更优雅的解决方案。
Let's start with the code ...
让我们从代码开始......
class SourceType
{
public int Id;
public string Name;
// other properties
}
class DestinationType
{
public int Id;
public string Name;
// other properties
}
List<SourceType> sourceList = new List<SourceType>();
sourceList.Add(new SourceType { Id = 1, Name = "1111" });
sourceList.Add(new SourceType { Id = 2, Name = "2222" });
sourceList.Add(new SourceType { Id = 3, Name = "3333" });
sourceList.Add(new SourceType { Id = 5, Name = "5555" });
List<DestinationType> destinationList = new List<DestinationType>();
destinationList.Add(new DestinationType { Id = 1, Name = null });
destinationList.Add(new DestinationType { Id = 2, Name = null });
destinationList.Add(new DestinationType { Id = 3, Name = null });
destinationList.Add(new DestinationType { Id = 4, Name = null });
I would like to achieve the following:
我想实现以下目标:
- destinationList should be filled with Names of corresponding entries (by Id) in sourceList
- destinationList should not contain entries that are not present in both lists at once (eg. Id: 4,5 should be eliminated) - something like inner join
- I would like to avoid creating new destinationList with updated entries because both lists already exist and are very large, so no "convert" or "select new".
- destinationList 应填写 sourceList 中相应条目的 Names(按 Id)
- destinationList 不应包含同时出现在两个列表中的条目(例如,应消除 Id: 4,5) - 类似于内部联接
- 我想避免使用更新的条目创建新的 destinationList,因为两个列表都已经存在并且非常大,所以没有“转换”或“选择新的”。
In the end destinationList should contain:
最后 destinationList 应包含:
1 "1111"
2 "2222"
3 "3333"
Is there some kind of elegant (one line Lambda? ;) solution to this using LINQ ?
是否有某种优雅的(一行 Lambda?;)使用 LINQ 解决这个问题?
Any help will be greatly appreciated! Thanks!
任何帮助将不胜感激!谢谢!
采纳答案by BrokenGlass
I would just build up a dictionary and use that:
我只想建立一个字典并使用它:
Dictionary<int, string> map = sourceList.ToDictionary(x => x.Id, x => x.Name);
foreach (var item in destinationList)
if (map.ContainsKey(item.Id))
item.Name = map[item.Id];
destinationList.RemoveAll(x=> x.Name == null);
回答by jason
Frankly, this is the simplest:
坦率地说,这是最简单的:
var dictionary = sourceList.ToDictionary(x => x.Id, x => x.Name);
foreach(var item in desitnationList) {
if(dictionary.ContainsKey(item.Id)) {
item.Name = dictionary[item.Id];
}
}
destinationList = destinationList.Where(x => x.Name != null).ToList();
You could do something ugly with Joinbut I wouldn't bother.
你可以做一些丑陋的事情,Join但我不会打扰。
回答by amit_g
Barring the last requirement of "avoid creating new destinationList" this should work
除了“避免创建新的目的地列表”的最后一个要求,这应该有效
var newList = destinationList.Join(sourceList, d => d.Id, s => s.Id, (d, s) => s);
To take care of "avoid creating new destinationList", below can be used, which is not any different than looping thru whole list, except that it probably is less verbose.
为了解决“避免创建新的目标列表”,可以使用下面的方法,这与循环遍历整个列表没有什么不同,只是它可能不那么冗长。
destinationList.ForEach(d => {
var si = sourceList
.Where(s => s.Id == d.Id)
.FirstOrDefault();
d.Name = si != null ? si.Name : "";
});
destinationList.RemoveAll(d => string.IsNullOrEmpty(d.Name));
回答by Pablushka
I hope this will be useful for you. At the end, destinationList has the correct data, without creating any new list of any kind.
我希望这对你有用。最后,destinationList 具有正确的数据,而无需创建任何类型的新列表。
destinationList.ForEach(x =>
{
SourceType newSource = sourceList.Find(s=>s.Id == x.Id);
if (newSource == null)
{
destinationList.Remove(destinationList.Find(d => d.Id == x.Id));
}
else
{
x.Name = newSource.Name;
}
});
回答by Towhidul Islam Tuhin
Hope this will your desired result. First join two list based on key(Id) and then set property value from sourceList.
希望这将是您想要的结果。首先根据 key(Id) 连接两个列表,然后从 sourceList 设置属性值。
var result = destinationList.Join(sourceList, d => d.Id, s => s.Id, (d, s) =>
{
d.Name = s.Name;
return d;
}).ToList();

