【发布时间】:2016-12-19 12:10:53
【问题描述】:
我有两个列表
List<string[]> List1;
List<string[]> List2;
List1 如下所示:
SECTION BEGINNING Value Ending Value
section2 31507976.71 0
section6 31643256.16 0
section8 32297021.88 0
section14 31643256.16 0
列表2:
SECTION Ending Value
section2 31406327.47
section8 33863875.36
section10 32674862.89
我想根据每个列表第一列的值将 list2 中的项目添加到 list1。 List1 应如下所示:
SECTION BEGINNING Value Ending Value
section2 31507976.71 31406327.47
section6 31643256.16 0
section8 32297021.88 33863875.36
section14 31643256.16 0
这是我的代码:
public static void getList()
{
var list1 = new[]
{
new {Section = "section2", BeginningValue = 31507976.71, EndingValue=0},
new {Section = "section6", BeginningValue = 31643256.16, EndingValue=0},
new {Section = "section8", BeginningValue = 32297021.88, EndingValue=0},
new {Section = "section14", BeginningValue = 31643256.16, EndingValue=0},
};
var list2 = new[]
{
new {Section = "section2", EndingValue=31406327.47},
new {Section = "section8", EndingValue=33863875.36},
new {Section = "section10", EndingValue=32674862.89},
};
var result = list1.Concat(list2).OrderByDescending(x => x.EndingValue).GroupBy(g => x.Section).ToList();
foreach (var item in result)
Console.WriteLine(item.Section + "-" + item.BeginningValue + "-" + item.EndingValue);
}
【问题讨论】:
-
语言是强类型是有原因的,这段代码很模糊
-
您使用专门匿名对象的任何特殊原因?
-
当
BEGINNING Value或ENDING VALUE来自同一个SECTION不同时会发生什么?