C# - 从另一个列表中使用LINQ填充列表<>

时间:2015-11-19 11:39:33

标签: c# linq

这是我的主要课程:

public class Subject
{
    public struct Class
    {
        public byte Day { get; set; }
        public DateTime Time { get; set; }
    }

    public string Name { get; set; }

    public List<Class> Data { get; set; }
}

例如,

List<Subject> subjects = new List<Subject>
        {
            new Subject()
            {
                Name = "Math",
                Data = new List<Class>()
                {
                    new Class { Day = 2, Time = Convert.ToDateTime("8:30") },
                    new Class { Day = 2, Time = Convert.ToDateTime("10:25") }
                }
            },
            new Subject()
            {
                Name = "Astronomy",
                Data = new List<Class>()
                {
                    new Class { Day = 2, Time = Convert.ToDateTime("12:30") },
                    new Class { Day = 4, Time = Convert.ToDateTime("14:30") }
                }
            },
            new Subject()
            {
                Name = "Chemistry",
                Data = new List<Class>()
                {
                    new Class { Day = 3, Time = Convert.ToDateTime("8:30") }
                }
            },
            new Subject()
            {
                Name = "Physics",
                Data = new List<Class>()
                {
                    new Class { Day = 3, Time = Convert.ToDateTime("10:25") },
                    new Class { Day = 4, Time = Convert.ToDateTime("12:30") }
                }
            }
        };

通过解析JSON填补了上述数据。 但现在我需要做下一步:

1.选择所有不同的Day(在这种情况下:2,3,4); 2.这几天填写主题(NameTime)。

我创造了这个:

public class Schedule
{
    public struct Subject
    {
        public string Name { get; set; }
        public DateTime Time { get; set; }
    }

    public struct Day
    {
        public byte DayOfWeek { get; set; }
        public List<Subject> Subjects { get; set; }
    }

    public List<Day> Days { get; set; }
}

所以我除了做这样的事情:

Schedule schedule = new Schedule();
schedule.Days = new List<Schedule.Day>()
{
    new Schedule.Day()
    {
        DayOfWeek = 2,
        Subjects = new List<Schedule.Subject>()
        {
            new Schedule.Subject() { Name = "Math", Time = Convert.ToDateTime("8:30") },
            new Schedule.Subject() { Name = "Math", Time = Convert.ToDateTime("10:25") },
            new Schedule.Subject() { Name = "Astronomy", Time = Convert.ToDateTime("12:30") }
        }
    },
    new Schedule.Day()
    {
        DayOfWeek = 3,
        Subjects = new List<Schedule.Subject>()
        {
            new Schedule.Subject() { Name = "Chemistry", Time = Convert.ToDateTime("8:30") },
            new Schedule.Subject() { Name = "Physics", Time = Convert.ToDateTime("10:25") },
        }
    },
    new Schedule.Day()
    {
        DayOfWeek = 4,
        Subjects = new List<Schedule.Subject>()
        {
            new Schedule.Subject() { Name = "Physics", Time = Convert.ToDateTime("12:30") },
            new Schedule.Subject() { Name = "Astronomy", Time = Convert.ToDateTime("14:30") }
        }
    }
};

问题是如何使用LINQ Schedule选择数据List<Subjects>(我不想使用循环)。

4 个答案:

答案 0 :(得分:2)

        var result = subjects
            .SelectMany(s => s.Data.Select(x => new { s.Name, x.Day, x.Time }))
            .GroupBy(x => x.Day)
            .Select(g => new Schedule.Day
                {
                    DayOfWeek = g.Key,
                    Subjects = g.Select(item => new Schedule.Subject
                        {
                            Name = item.Name,
                            Time = item.Time
                        })
                        .OrderBy(item => item.Time)
                        .ToList()
                })
                .OrderBy(gItem => gItem.DayOfWeek)
            .ToList();

然后......

Schedule schedule = new Schedule();
schedule.Days = result;

List<Day>放入日程安排课程中也很奇怪。

答案 1 :(得分:1)

没有乐趣我已经重写了解决方案以推广查询表达式语法。

second

答案 2 :(得分:0)

如果我理解,你可以使用ForEach LINQ:

schedule.Days.ForEach(x => x.Subjects.ForEach(y => Console.WriteLine($"{y.Name}, {y.Time}")));
而不是

 Console.WriteLine($"{y.Name}, {y.Time}")

您可以将此信息写入一些变量。

P.S。 ForEach将传递列表的所有元素。

答案 3 :(得分:0)

试试这个:

var newQuery = subjects.SelectMany(y => y.Data.Select(x=> new {x.Day,x.Time,y.Name }));
var newQuery2 = newQuery.OrderBy(x=>x.Day).OrderBy(x=>x.Time).GroupBy(x => x.Day);
var newQuery3 = newQuery2.Select(x => new Schedule.Day
            {
                DayOfWeek = x.Key,
                Subjects = x.Select(y => new Schedule.Subject
                {
                    Time = y.Time,
                    Name = y.Name
                }).ToList()
            });