C#linq构建表来自列表的子列表

时间:2016-09-28 06:16:21

标签: c# linq

在linq查询的结果中,我希望得到List<Record> records,其中应包含以下内容:

Id=1, Number=1,Weight=23,Street="Testing street"

Id=1, Number=1,Weight=23,Street="Testing street"

Id=1, Number=1,Weight=23,Street="Testing street"

Id=1, Number=1,Weight=23,Street="Testing street"

Id=1, Number=1,Weight=23,Street="Testing street"

Id=1, Number=1,Weight=23,Street="Testing street"

代码:

namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Train> Depos = new List<Train>();
            Train train = new Train();
            train.Id = 1;
            List<Wagon> wagons = new List<Wagon>();
            wagons.Add(new Wagon { Number = 1, Weight = 23 });
            wagons.Add(new Wagon { Number = 1, Weight = 23 });
            wagons.Add(new Wagon { Number = 1, Weight = 23 });
            wagons.Add(new Wagon { Number = 1, Weight = 23 });
            wagons.Add(new Wagon { Number = 1, Weight = 23 });
            wagons.Add(new Wagon { Number = 1, Weight = 23 });
            train.Wagons = wagons;
            Depos.Add(train);

            List<Adress> Adresses = new List<Adress>();
            Adress adress = new Adress();
            adress.Id = 1;
            adress.Street = "Testing street";
            Adresses.Add(adress);
            List<Record> records = (from dep in Depos join adr in Adresses on dep.Id equals adr.Id select new Record { Id = dep.Id }).ToList();
        }
    }
    public class Record
    {
        public int Id { get; set; }

        public int Weight { get; set; }

        public string Street { get; set; }

        public int Number { get; set; }
    }

    public class Train
    {
        public int Id { get; set; }

        public List<Wagon> Wagons { get; set; }
    }

    public class Wagon
    {
        public int Weight { get; set; }

        public int Number { get; set; }
    }

    public class Adress
    {
        public int Id { get; set; }
        public string Street { get; set; }
    }
}

这样做的最佳方式是什么?

2 个答案:

答案 0 :(得分:3)

您需要使用SelectMany,或者在查询语法中使用第二个from来展平您的列表列表:

  List<Record> records =(from dep in Depos 
                         join adr in Adresses on dep.Id equals adr.Id 
                         from wgn in dep.Wagons
                         select new Record 
                         { 
                            Id = dep.Id, 
                            Number = wgn.Number, 
                            Weight = wgn.Weight, 
                            Street = adr.Street 
                          }).ToList();

答案 1 :(得分:3)

可读性较差的LINQ和扩展方法:)

IEnumerable<Record> records = 
    Depos.Join(Adresses, 
               myTrain => train.Id, 
               addr => addr.Id, 
               (myTrain, addr) => myTrain.Wagons
                                         .Select(wagon => new Record 
                                         { 
                                             Id = addr.Id, 
                                             Street = addr.Street, 
                                             Number = wagon.Number, 
                                             Weight = wagon.Weight 
                                         }))
         .SelectMany(myWagons => myWagons);

如您所见SelectMany方法是此查询的关键方法