也许现在已经很晚了,但我在这里被卡住了。我正在尝试将具有相同属性的多个列表合并为一个。我以为LINQ的.UNION能做到这一点,但我错了。下面是我的一些列表示例:
LIST1 (report names):
Date Name Title Product
02/01/13 Steve Hello World Report
02/05/13 Greg Howdy Report
LIST2 (song names):
Date Name Title Product
01/01/13 John Time Song
01/05/13 Bob Sorry Song
LIST3 (games names):
Date Name Title Product
12/01/12 Google Bike Race Game
12/05/12 Apple Temple Run Game
我的课程非常简单。以下是它的样子:
public class MyClass {
public DateTime Date { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Product { get; set; }
}
如果你想知道,我使用了这个 LINQ 查询来获取上面列表中的一个:
var finalList = Games
.Select (s => new MyClass {
Date = (System.DateTime) s.Games.Creation_date,
Name = s.Games.Last_name,
Title = string.Format("{0} (Report)", s.Game.Headline),
Product="Report"
})
;
到目前为止,这很容易,但我想把所有的列表合并成一个。因此,我的最终列表应该如下所示:
Date Name Title Product
02/01/13 Steve Hello World Report
02/05/13 Greg Howdy Report
01/01/13 John Time Song
01/05/13 Bob Sorry Song
12/01/12 Google Bike Race Game
12/05/12 Apple Temple Run Game
我曾以为一个 UNION 命令就能解决这个问题:
var newList = List1.Union(List2).Union(List3);
但我没有得到期望的输出结果。
Date Name Title Product
02/01/13 Steve Hello World Report
02/05/13 Greg Howdy Report
01/01/13 Bob Time Game
01/05/13 John Sorry Song
12/01/12 Google Bike Race Song
12/05/12 Apple Temple Run Game
有什么想法,我在这里做错了什么吗?