首页 > 解决方案 > 从源列表中获取由分组项目列表组成的列表

问题描述

所以我有一个来自数据库的源列表,我得到了这样的结果。

List<FileInfo> FileInfosList = DataBase.GetFileInfoslist(LoginInfo);

这将返回一个包含大约 50 行 FileInfo 类型的列表。列表中的每一行有 10 个字段,两个是 starttime 和 index

double starttime is starttime of media
int index is a index number from 0 to 3

我像这样对列表进行排序

List<FileInfo> sortedList = FileInfos.OrderBy(s => s.StartTime).ThenBy(c => c.Index).ToList();

所以我需要从这个排序列表中提取行组,其中开始时间在 50 行或记录中匹配。每组可能有 1 到 4 行。而且我需要留下一个具有匹配开始时间的组列表。

例如,如果我想提取与列表中第一行的开始时间匹配的一组行,我将使用

 double starttime = sortedList[0].StartTime;
 List<FileInfo>() group = sortedList.Where(x => x.StartTime == 
 starttime).ToList();

但我需要一种方法来遍历整个排序列表并获取具有匹配开始时间的所有组,每个组可以是 1 到 4 行(其变量),索引号保持在 0 到 3 的排序顺序中。所有这些然后组需要在我可以使用的主列表中。

所以像

List<FileInfo> GroupOfFileInfo = list of rows with matching starttimes from source list

我想结束

List<GroupOfFileInfo> groupslist for the entire source list.

我希望这很清楚。

谢谢

标签: c#linq

解决方案


正如评论所说,使用 .GroupBy()。

GroupBy 使用起来可能有点混乱,所以这里有一个示例控制台应用程序:

class Program
{

    //Simple 'file info' class
    public class FileInfo
    {
        public int Index { get; set; }
        public DateTime StartTime { get; set; }
        public string OtherInfo { get; set; }
    }

    //Set up example data.
    static List<FileInfo> data = new List<FileInfo>()
    {
        new FileInfo{ Index = 0, StartTime = new DateTime(2019,01,01), OtherInfo="Item 0" },
        new FileInfo{ Index = 1, StartTime = new DateTime(2019,01,01), OtherInfo="Item 1" },
        new FileInfo{ Index = 2, StartTime = new DateTime(2019,01,01), OtherInfo="Item 2" },
        new FileInfo{ Index = 3, StartTime = new DateTime(2019,02,01), OtherInfo="Item 3" },
        new FileInfo{ Index = 4, StartTime = new DateTime(2019,02,01), OtherInfo="Item 4" },
        new FileInfo{ Index = 5, StartTime = new DateTime(2019,02,01), OtherInfo="Item 5" },
        new FileInfo{ Index = 6, StartTime = new DateTime(2019,03,01), OtherInfo="Item 6" },
        new FileInfo{ Index = 7, StartTime = new DateTime(2019,04,01), OtherInfo="Item 7" }
    };

    static void Main(string[] args)
    {
        //First GroupBy what you want to group by.
        var groupedList = data.GroupBy(x => x.StartTime);

        //This gives a list with the number of distinct group items.
        foreach (var groupEntry in groupedList)
        {
            //groupEntry is an IGrouping, which contains 'key' as the group key
            //(a DateTime in this case, StartTime in the source list)
            Console.WriteLine($"Items in Group : {groupEntry.Key}");
            //each item is itself an IEnumerable of the items in that group
            foreach (var groupItem in groupEntry.OrderBy(x=>x.Index))
            {
                Console.WriteLine($"  - Index:{groupItem.Index}; OtherInfo = {groupItem.OtherInfo}");
            }
        }
    }
}

上述输出:

Items in Group : 01/01/2019 00:00:00
  - Index:0; OtherInfo = Item 0
  - Index:1; OtherInfo = Item 1
  - Index:2; OtherInfo = Item 2
Items in Group : 01/02/2019 00:00:00
  - Index:3; OtherInfo = Item 3
  - Index:4; OtherInfo = Item 4
  - Index:5; OtherInfo = Item 5
Items in Group : 01/03/2019 00:00:00
  - Index:6; OtherInfo = Item 6
Items in Group : 01/04/2019 00:00:00
  - Index:7; OtherInfo = Item 7

推荐阅读