C# 第一条和最后一条记录Linq值之间的差异

C# 第一条和最后一条记录Linq值之间的差异,c#,linq,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/

我的列表中有如下数据

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
我想根据每个profilename的日期计算第一条记录和最后一条记录之间的分数差。所需输出为

prof1 9
prof2 29
prof3 72

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

不是最好的性能,但如果您关心性能,请不要使用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();
.GroupBy(e=>e.Group)。选择(e=>newkeyvaluepair(e.Key,e.OrderBy(f=>f.Date)。Last().Num-e.OrderBy(f=>f.Date)。First().Num)).ToList();

不是最好的性能,但是如果您关心性能,就不要使用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();
.GroupBy(e=>e.Group)。选择(e=>newkeyvaluepair(e.Key,e.OrderBy(f=>f.Date)。Last().Num-e.OrderBy(f=>f.Date)。First().Num)).ToList();

您可以根据
档案名称和计算差异进行分组,如:

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();

您可以按
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();
我想根据每个profilename的日期计算第一条记录和最后一条记录之间的分数差

因此,您需要按
profilename
进行分组,按
日期对每个组进行排序,取第一个和最后一个项目并计算差异

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();
我想根据每个profilename的日期计算第一条记录和最后一条记录之间的分数差

因此,您需要按
profilename
进行分组,按
日期对每个组进行排序,取第一个和最后一个项目并计算差异

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();
这似乎有效(下面的完整代码,使用
DateTime
):

下面是一个包含您的数据的可编译示例:

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();
}
课率
{
公共费率(字符串p、字符串d、整数s)
{
profileName=p;
date=DateTime.Parse(d);
得分=s;
}
公共字符串profileName;
公共日期时间日期;
公众智力得分;
}
void Main()
{
var list=新列表()
{ 
新费率(“prof1”、“2015年10月1日”、“15日”),
新费率(“prof1”、“2015年10月2日”和“15”),
新费率(“prof1”、“2015年10月3日”18),
新费率(“prof1”、“2015年10月25日”、“24日”),
新费率(“prof2”、“01/10/2015”、69),
新费率(“prof2”,“2015年10月2日”,70),
新费率(“prof3”,“01/10/2015”,115),
新费率(“prof3”,“02/10/2015”,115),
新费率(“prof2”、“2015年10月3日”,73),
新费率(“prof3”,“03/10/2015”,119),
新费率(“prof2”、“2015年10月25日”、98),
新费率(“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();
}
这似乎有效(下面是完整的代码,使用
DateTime
):

下面是一个包含您的数据的可编译示例:

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();
}
课率
{
公共费率(字符串p、字符串d、整数s)
{
profileName=p;
date=DateTime.Parse(d);
得分=s;
}
公共字符串profileName;
公共日期时间日期;
公众智力得分;
}
void Main()
{
var list=新列表()
{ 
新费率(“prof1”、“2015年10月1日”、“15日”),
新费率(“prof1”、“2015年10月2日”和“15”),
新费率(“prof1”、“2015年10月3日”18),
新费率(“prof1”、“2015年10月25日”、“24日”),
新费率(“prof2”、“01/10/2015”、69),
新费率(“prof2”,“2015年10月2日”,70),
新费率(“prof3”,“01/10/2015”,115),
新费率(“prof3”,“02/10/2015”,115),
新费率(“prof2”、“2015年10月3日”,73),
新费率(“prof3”,“03/10/2015”,119),
新费率(“prof2”、“2015年10月25日”、98),
新费率(“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();
}

如果是评论中提到的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();

如果是评论中提到的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();

什么类型的“日期”
string
或'DateTime'?
string
数据类型它是“Downvoter”,是否要评论downvote的原因?在stackoverflow上,在提问之前,您需要先付出一些努力。向我们展示到目前为止的代码-您尝试了什么?对于prof3,您的预期输出似乎是错误的。查看fiddle它需要69而不是72。什么类型的“日期”
string
或'DateTime'?
string
数据类型它是“Downvoter”,是否要评论downvote的原因?在stackoverflow上,在提问之前,您需要先付出一些努力。向我们展示到目前为止的代码-您尝试了什么?对于prof3,您的预期输出似乎是错误的查看fiddle它需要69而不是72。需要更多换行符。需要更多换行符。