Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq groupby返回唯一组_C#_Linq_List_Group By - Fatal编程技术网

C# Linq groupby返回唯一组

C# Linq groupby返回唯一组,c#,linq,list,group-by,C#,Linq,List,Group By,我有记录列表,每条记录都有名称和轮次。 轮数是由“-” 如何按名称分组并仅显示唯一的轮数,如何计算轮数并显示第一轮和最后一轮 这是我试过的 data.GroupBy(d => d.Name) .Select( g => new { Name = g.Key, Rounds = g.Concat(s => s.Rounds), NumberOfRounds = g.Concat(s => s.Round

我有记录列表,每条记录都有名称轮次轮数是由“-”

如何按名称分组并仅显示唯一的轮数,如何计算轮数并显示第一轮和最后一轮

这是我试过的

 data.GroupBy(d => d.Name)
.Select(
    g => new
    {
        Name = g.Key,
        Rounds = g.Concat(s => s.Rounds),
        NumberOfRounds =  g.Concat(s => s.Rounds).Split('-').Count,
        FirstRound = //??,
        LastRound = //??,
    });

首先,需要将轮数提取为数值。在那之后,就很容易了

var data = ...;
var groupedData = data
    .GroupBy(x => x.Name)
    .Select(x => new {
        Name = x.Key,
        Rounds = string.Join("-", x.Select(z => z.Rounds))
            .Split('-')
            .Distinct()
            .Select(z => int.Parse(z))
            .OrderBy(z => z)
            .ToArray()
    })
    .Select(x => new {
        x.Name,
        Rounds = string.Join("-", x.Rounds),
        NumberOfRounds = x.Length,
        FirstRound = x.Min(),
        LastRound = x.Max()
    })
    .ToArray();
首先,我将把您的实体投影到一个名称和一对圆的集合中。这样就更容易合作了。例如:

var query = results
    .Select(d => new { d.Name, Results = d.Rounds.Split('-').Select(int.Parse).ToList() })
    .GroupBy(
        d => d.Name, (key, values) => new {
            Name = key,
            Rounds = values.SelectMany(v => v.Rounds)
                           .Distinct()
                           .OrderBy(x => x)
                           .ToList()
       });
由于轮数作为列表提供,我认为将
numberofhards
FirstRound
LastRound
作为属性没有多大意义,因为您可以使用
rounds.Count
rounds.First()、
rounds.Last()
。重要的部分是尽早将数据转换为更有用的格式

如果您真的需要在属性中使用它,则可以很容易地投影:

// query as before, but with
.Select(x => new {
    x.Name,
    x.Rounds,
    NumberOfRounds = x.Rounds.Count,
    FirstRound = x.Rounds.First(),
    LastRound = x.Rounds.Last()
});

使用
.SelectMany()
我不在电脑前,否则我会发布答案。@juharr括号在哪里?而且
.ToArray()
不是
字符串.Join(…)
调用的一部分。第一个
Select
中的
x
是一个
i分组
,因此它没有
Rounds
属性。@Maarten,谢谢你回答我的问题!