LINQ Query数据表问题

时间:2017-11-07 18:34:25

标签: asp.net entity-framework linq linq-to-sql

我现在有一个看起来像这样的DataTable

California  2018    v32
Texas       2017    v31
New Jersey  2017    v31
Maine       2017    v31
New York    2014    v31
Alaska      2010    v29

我希望输出使用LINQ查询

v32
     2018
     California

v31
     2017
     Texas
     New Jersey
     Maine

    2014
    New York

v29
    2010
    Alaska

非常感谢任何帮助。此数据表将附加到asp.net转发器控件

1 个答案:

答案 0 :(得分:1)

    public class Faker
    {
        public string State { get; set; }
        public int Year { get; set; }
        public string Version { get; set; }

        public Faker(string state, int year, string version)
        {
            State = state;
            Year = year;
            Version = version;
        }
    }

    static void Main(string[] args)
    {
        var ls = new List<Faker>
        {
            new Faker("California", 2018, "v32"),
            new Faker("Texas", 2017, "v31"),
            new Faker("New Jersey", 2017, "v31"),
            new Faker("Maine", 2017, "v31"),
            new Faker("New York", 2014, "v31"),
            new Faker("Alaska", 2010, "v29"),
        };


        ls.GroupBy(x => x.Version)
            .ToList()
            .ForEach(x => 
                {
                Console.WriteLine(x.Key);

                x.GroupBy(y => y.Year)
                    .ToList()
                    .ForEach(y => {
                    Console.WriteLine($"\t{y.Key}");

                        y.GroupBy(z => z.State)
                            .ToList()
                            .ForEach(z => Console.WriteLine($"\t{z.Key}"));
                         Console.WriteLine();
                    });
                });

        Console.ReadLine();
    }

enter image description here