1

我有一个包含以下类的列表

    public class File
{
    public string Name{get;set;}
    public int Year{get;set;}
    public decimal Amount{get;set;}

}

我的列表可能包含即列表

Name="File 1" Year=2011 Amount=200
Name="File 2" Year=2012 Amount=400
Name="File 3" Year 2013 Amount=500

我希望将上面的列表转换为包含以下值

Name="File 1" Year=2011 Amount=200
Name="File 1" Year=2012 Amount=0
Name="File 1" Year 2013 Amount=0
Name="File 2" Year=2011 Amount=400
Name="File 2" Year=2012 Amount=0
Name="File 2" Year 2013 Amount=0
Name="File 3" Year 2011 Amount=0
Name="File 3" Year=2012 Amount=0
Name="File 3" Year 2013 Amount=500

注意其他金额为0的项目是为缺失年份自动生成的

4

2 回答 2

3
foreach(var name in files.Select(x => x.Name).Distinct())
foreach(var year in files.Select(x => x.Year).Distinct())
{
    var amount = (from file in files
                  where file.Name == name
                     && file.Year == year
                  select (decimal?)file.Amount).SingleOrDefault() ?? 0M;
    Console.WriteLine("Name={0}, Year={1}, Amount={2}",
                  name, year, amount);
}

显然它可以被优化——尤其是作为通过字典查找数量的时候;也许:

var amounts = files.ToDictionary(
        file => new {file.Name,file.Year}, file => file.Amount);

foreach(var name in files.Select(file => file.Name).Distinct())
foreach(var year in files.Select(file => file.Year).Distinct())
{
    decimal amount;
    if(!amounts.TryGetValue(new {file.Name,file.Year}, out amount) amount = 0M;
    Console.WriteLine("Name={0}, Year={1}, Amount={2}",
                  name, year, amount);
}
于 2013-03-08T11:42:17.667 回答
0

对于默认值,您可以添加一个构造函数:

public class File
{
    public File() { this.Amout = 0; }

    public string Name{get;set;}
    public int Year{get;set;}
    public decimal Amount{get;set;}
}

对于 OrderBy,您可以查看此示例

于 2013-03-08T11:40:37.663 回答