• 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}");
                }
    
            }
  • 相关阅读:
    数组从文件中读取(接上问题)
    符合json格式要求的字符串转化为json字符串
    json-lib --->入门
    XStream-->别名;元素转属性;去除集合属性(剥皮);忽略不需要元素
    Ajax案例5-->省市联动
    Ajax案例4-->接收后台传递的XML数据
    Ajax案例3-->判断用户名是否被占用
    Ajax案例2-->POST请求
    Ajax案例1-->GET请求
    SecureCRT连接VMWare中的linux系统相关配置
  • 原文地址:https://www.cnblogs.com/Zingu/p/16441823.html
Copyright © 2020-2023  润新知