Linq和Lamuda关联查询(Join)

Model:

    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public byte Age { get; set; }
        public string QQ { get; set; }
        public int UniversityID { get; set; }
    }
    class University
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

LoadData:

static void InitData()
        {
            students = new Student[]
            {
                new Student() { Id=1,Name="zhangsan",Age=20,QQ="3250001",UniversityID=1 },
                new Student() { Id=2,Name="lisi",Age=25,QQ="9520001" ,UniversityID=2},
                new Student() { Id=3,Name="wuwang",Age=30,QQ="10000852" ,UniversityID=1},
                new Student() { Id=4,Name="zhaoliu",Age=40,QQ="10056851",UniversityID=2 },
                new Student() { Id=5,Name="sunqi",Age=17,QQ="109145611",UniversityID=1 },
                new Student() { Id=6,Name="laoba",Age=49,QQ="38122551" ,UniversityID=3} 
            };
            university = new University[] {
                new University{ ID=1,Name="qinghua"},
                new University{ ID=2,Name="beijing"},
                new University{ ID=3,Name="fudan"}
            };
        } 

Linq:

static void LinqEx() 
        {
            var data = from student in students
                       join univer in university on student.UniversityID equals univer.ID
                       orderby student.Id
                       select new { ID = student.Id, Name = student.Name, QQ = student.QQ, University = univer.Name }; 
            foreach (var item in data)
            { 
                Type t = item.GetType();
                foreach (var property in t.GetProperties())
                {
                    Console.Write($"{property.Name}:{property.GetValue(item, null)}");
                }
                Console.WriteLine(); 
            } 
        } 

Lamuda:

   static void Lamuda()
        {
            var data = students.Join(university,
                stu => stu.UniversityID,
                uni => uni.ID,
                (st, uni) => new { ID = st.Id, Name = st.Name, QQ = st.QQ, University = uni.Name }
                ).ToList().Where(t=>t.QQ.StartsWith("1"));
            foreach (var item in data)
            {
                Console.WriteLine($" ID= {item.ID},Name={item.Name},QQ={item.QQ}, University ={item.University}");
            }
        }

LinqToXML:

        static void LinkToXML()
        {
            string xmlData = @$"<Students>
                                    <Student>
                                       <Name>zhangsan</Name>                       
                                       <Age>32</Age>
                                       <University>beijing</University>
                                    </Student>
                                    <Student>
                                       <Name>lisi</Name>                       
                                       <Age>23</Age>
                                       <University>qinghua</University>
                                    </Student>
                                    <Student>
                                       <Name>wangwu</Name>                       
                                       <Age>25</Age>
                                       <University>fudan</University>
                                    </Student>
                                </Students>  ";

            XDocument xDocument = new XDocument();
            xDocument= XDocument.Parse(xmlData);
            var student = xDocument.Descendants("Student")
                .Select(t =>
                        new
                        {
                            Name = t.Element("Name").Value,
                            Age= t.Element("Age").Value,
                            University = t.Element("University").Value,
                        });

            foreach (var item in student)
            {
                Console.WriteLine($"Name:{item.Name} Age:{item.Age} University:{item.University}");
            }

        }

 

posted @ 2022-07-04 09:37  后跳  阅读(112)  评论(0编辑  收藏  举报