Linq的第一个和最后一个记录的值之间的差异

时间:2015-11-30 09:59:01

标签: c# linq

我的列表中有数据如下

profilename    date        score
prof1          01/10/2015  15
prof1          02/10/2015  15
prof1          03/10/2015  18
....
prof1          25/10/2015  24
prof2          01/10/2015  69
prof2          02/10/2015  70
prof3          01/10/2015  115
prof3          02/10/2015  115
prof2          03/10/2015  73
prof3          03/10/2015  119
....
prof2          25/10/2015  98
prof3          25/10/2015  187

我想计算每个个人资料名称的第一个和最后一个记录之间的分数差异。所需的输出是

prof1 9
prof2 29
prof3 72

我不知道如何在Linq中开始这个查询,因为我是新手。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

  

我想计算每个个人资料名称的第一个和最后一个记录之间的分数差异。

因此,您需要按profilename 分组排序每个组date,获取第一个和最后一个项目并计算差异< / p>

var result =
    (from item in list
     group item by item.profilename into g
     let groupItems = g.OrderBy(gi => Convert.ToDateTime(gi.date))
     select new { profilename = g.Key, score = groupItems.Last().score - groupItems.First().score }
    ).ToList();

答案 1 :(得分:1)

您可以按profilename分组并计算差异,如:

var result = list.GroupBy(c => c.profilename).
             Select(c => new 
                    { 
                       profilename = c.Key, 
                       score = c.FirstOrDefault(m => m.date == c.Max(v => v.date)).score - 
                               c.FirstOrDefault(m => m.date == c.Min(v => v.date)).score 
                    }).ToList();

答案 2 :(得分:1)

如果是评论中提到的Linq-To-Sql,这可能是最好的方法:

var result = profiles
        .GroupBy(p => p.profilename)
        .Select(g => new {
            profilename = g.Key,
            firstscore = g.OrderBy(p => p.date).First().score,
            lastscore = g.OrderByDescending(p => p.date).First().score
        })
        .Select(x => new { x.profilename, diff = x.lastscore - x.firstscore })
        .ToList();

Demo fiddle

答案 3 :(得分:0)

不是最佳性能,但如果您关心性能,请不要使用LINQ

.GroupBy(e => e.Group).Select(e => new KeyValuePair<string, int>(e.Key, e.OrderBy(f => f.Date).Last().Num - e.OrderBy(f => f.Date).First().Num)).ToList();

答案 4 :(得分:0)

这似乎有用(下面的完整代码,使用DateTime):

var l = list.OrderBy(i => i.date)
            .GroupBy(i => i.profileName)
            .Select(e => new {prof = e.First().profileName, diff = e.Last().score - e.First().score}).ToList();

以下是包含您的数据的可编辑示例:

class Rate
{
    public Rate(string p, string d, int s)
    {
        profileName = p;
        date = DateTime.Parse(d);
        score = s;
    }

    public string profileName;
    public DateTime date;
    public int score;
}

void Main()
{
    var list = new List<Rate>()
    { 
        new Rate("prof1", "01/10/2015", 15),
        new Rate("prof1", "02/10/2015", 15),
        new Rate("prof1", "03/10/2015", 18),

        new Rate("prof1", "25/10/2015", 24),
        new Rate("prof2", "01/10/2015", 69),
        new Rate("prof2", "02/10/2015", 70),
        new Rate("prof3", "01/10/2015", 115),
        new Rate("prof3", "02/10/2015", 115),
        new Rate("prof2", "03/10/2015", 73),
        new Rate("prof3", "03/10/2015", 119),

        new Rate("prof2", "25/10/2015", 98),
        new Rate("prof3", "25/10/2015", 184),
    };

    var l = list.OrderBy(i => i.date)
                .GroupBy(i => i.profileName)
                .Select(e => new {prof = e.First().profileName, diff = e.Last().score - e.First().score}).ToList();
}