public class Note
{
public string account;
public DateTime noteDate;
public string noteText;
}
List<Note> List1;
使用LINQ我正在寻找一种方法来合并noteText
account
和noteDate
相同的方式。例如,如果传入列表有三个对象:
List1[0] = "1234", 2017-10-03, "This is a"
List1[1] = "1234", 2017-10-03, " sample note."
List1[2] = "4321", 2017-10-03, "This is another note."
我希望新列表有两个对象:
List2[0] = "1234", 2017-10-03, "This is a sample note."
List2[1] = "4321", 2017-10-03, "This is another note."
答案 0 :(得分:5)
最简单的方法是按日期和帐户分组,然后Join
字符串字段:
List2 = List1.GroupBy(n => new {n.noteDate, n.account})
.Select(g => new Note {
noteDate = g.Key.noteDate,
account = g.Key.account,
noteText = string.Join("", g.Select(n => n.noteText))
});