0
public class ConvertedReading
{
    public int DatePointID { get; set; }
    public string DatapointName { get; set; }
    public DateTime ReadingDate { get; set; }
    public double Value { get; set; }
}

给定以下集合:

    IEnumerable<ConvertedReading> readingData;

使用 c# ,使用 lambda 语法编写一个 LINQ 语句以输出以下内容的集合:

public class DataPointTotal
{
    public string DatapointName{get; set;}
    public double Total{get; set;}
}

每件应退回一件datepointID

Total 应该表示convertedReading.valye每个相关的总和datePointID

我已经尝试过以下方法,如果它有效,我似乎无法得到正确的价值总和。

var subtotals = from x in ConvertedReading group x by x.DatapointName into g select new { Type = x.DatapointName , SubTotal = g.Sum(x => x.Total) };

语法是否正确?

多谢你们,

4

2 回答 2

2

答案是:

var result = readingData.GroupBy(r => r.DataPointID)
                        .Select(g => new DataPointTotal
                                {
                                    DataPointName = g.First().DataPointName,
                                    Total = g.Sum(r => r.Value)
                                });

但考虑到我写了这个问题,我想这是作弊!;)

于 2013-03-21T21:50:05.640 回答
0
var result = db.ConvertedReading.GroupBy(i=>i.DatapointName).Select(g => new DataPointTotal(){DatapointName = g.DatapointName, Total = g.Sum(i=>i.Total)});
于 2013-03-21T21:52:12.050 回答