zoukankan      html  css  js  c++  java
  • Linq in GroupBy GroupJoin

    还是上一份的代码例子:

      public class Person
        {
            public int ID { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }       
        }
        public class Dog
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int PersonID { get; set; }
        }
        public class DogClothes
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int DogID { get; set; }
        }
        public class ActionPerson
        { 
            public List<Person> GetPersons()
            {
                List<Person> list = new List<Person>();
                for (var i = 0; i < 10; i++)
                {
                    Person p = new Person {ID=i+1, Name = "hongda" + (i + 1), Age = 26 + i };
                    list.Add(p);
                }
                return list;
            }
            public List<Dog> GetDogs()
            {
                List<Dog> dogs = new List<Dog>();
                for (var i = 1; i < 15; i++)
                {
                    Dog dog = new Dog { ID = i, Name = "dogs" + i, PersonID = (i %5)+1 };
                    dogs.Add(dog);
                }
                return dogs;
            }
            public List<DogClothes> GetDogClotheses()
            {
                List<DogClothes> DogClothes = new List<DogClothes>();
                for (var i = 1; i < 13; i++)
                {
                    DogClothes clothes = new DogClothes() { ID = i, Name = "DogClothes" + i, DogID = (i % 4) + 2 };
                    DogClothes.Add(clothes);
                }
                return DogClothes;
            }
            public void Show<T>(List<T> list)
            {
                foreach (var l in list)
                {
                    ShowObj(l);
                }
            }
            public void ShowObj<T>(T data)
            {
                Type t = data.GetType();
                PropertyInfo[] infos = t.GetProperties();
                StringBuilder strBuilder = new StringBuilder();
                foreach (var p in infos)
                {
                    object value = p.GetValue(data, null);
                    string name = p.Name.ToString();
                    strBuilder.Append(name + ":" + value + ",");
                }
                Console.WriteLine(strBuilder.ToString());
            }
            public void ShowArray<T>(T[] arr)
            {
                foreach (var t in arr)
                {
                    Console.WriteLine(t.ToString());
                }
            }
        }

     操作:

     static void Main(string[] args)
            {
                ActionPerson ap = new ActionPerson();
                List<Person> list = ap.GetPersons();
                List<Dog> dogs = ap.GetDogs();
                List<DogClothes> dogClotheses = ap.GetDogClotheses();
                int[] arr = { 2, 3, 4, 5 };
                var result = list.Where(q => arr.Contains(q.ID));
                ap.Show(result.ToList ());
                Console.WriteLine("=============================");
                var result2 = list.Where(q => dogs.Select(o => o.PersonID).Contains(q.ID));
                ap.Show(result2.ToList());
                Console.ReadLine();
            }

     var result = list.Select(o => o.Name);
                ap.Show(result.ToList());

    再看

    var result = list.Select(o => o.Name);
                ap.ShowArray(result.ToArray());

    可以看出Select(o=>o.Name) 返回的是数组

    返回对象

       var result = list.Select(p => new { ID = p.ID, p.Name, p.Age });
                ap.Show(result.ToList ());

    对象数组

     var result = list.Select(p => new { ID = p.ID, p.Name, p.Age });
                ap.ShowArray(result.ToArray());

    Group Join

     var result = from a in list
                             join b in dogs on a.ID equals b.PersonID into c
                             select new { a, c };
                ap.Show(result.ToList());

      var result = from a in list
                             join b in dogs on a.ID equals b.PersonID into c
                             select new { a, c };
                foreach (var r in result)
                {
                    ap.ShowObj(r.a);
                    ap.Show(r.c.ToList());
                    Console.WriteLine("========================================");
                }

                var result = list.GroupJoin(dogs, a => a.ID, b => b.PersonID, (a, c) => new {a,c });

    结果同上

    c还有一些其他的属性 

    GroupBy

      var result = from b in dogs group b by b.PersonID into g select new { g.Key, Count = g.Count(), Member = g };
                foreach (var r in result)
                {
                    Console.WriteLine(r.Key + "," + r.Count + ",");
                    foreach (var m in r.Member)
                    {
                        ap.ShowObj(m);
                    }
                    Console.WriteLine("=================================");
                }

                 var result = (from b in dogs select b).GroupBy(q => q.PersonID).Select(q => new { q.Key, Count = q.Count(), Member = q });

    结果同上

  • 相关阅读:
    前后端渲染
    ES6模块化
    关于跨域请求
    移动端经常出现的兼容问题
    移动端开发
    4.sass的分支结构、循环结构、函数
    3.sass的数据类型与函数
    2.sass变量、嵌套、混合(mixin)、继承拓展、@import、comment
    1.sass的安装,编译,还有风格
    微信小程序上拉加载分页数据---包括tab选项卡切换(优化方案)
  • 原文地址:https://www.cnblogs.com/hongdada/p/3170026.html
Copyright © 2011-2022 走看看