C# Linq扩展方法案例

合并两个序列

var res = list1.Concat(list2);

去掉重复元素

var res = arr.Distinct();

筛选两个序列中的差异元素

// 筛选list1中与list2不相同的元素
var res = list1.Except(list2);

筛选以什么开头的信息

// 筛选list1中电话号码以178开头的
var res = list1.Where(q=>q.PhoneNo.StartsWith("178"));

将对象转换为字典集合

   Production[] prds =
   {
       new Production
       {
           PID = 4007,
           Name = "产品 1",
           Size = 123.45f,
           Quantity = 65
       },
       new Production
       {
           PID = 4008,
           Name = "产品 2",
           Size = 77.01f,
           Quantity = 100
       }
   };
   // 将PID作为key,Name作为value
   IDictionary<int, string> dic = prds.ToDictionary(p => p.PID, p => p.Name);

将原始数据进行分组

本例使用此重载形式:

public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Func<TKey, IEnumerable<TSource>, TResult> resultSelector
);
  • TSource是源数据中的元素,TKey是分组依据,TResult是返回调用方的元素类型。
  • keySelector用于产生分组依据。
  • resultSelector用于产生分组结果,它有两个参数,第一个是分组依据,即分组的"标题",第二个是隶属于该分组下的元素子序列。
    class Program
    {
        static void Main(string[] args)
        {
            Student[] stus =
            {
                new Student{ ID = 201, Name = "小王",  Course = "C"},
                new Student{ ID = 202, Name = "小曾",  Course = "C++"},
                new Student{ ID = 203, Name = "小吕",  Course = "C++"},
                new Student{ ID = 204, Name = "小孙",  Course = "C#"},
                new Student{ ID = 205, Name = "小郑",  Course = "C"},
                new Student{ ID = 206, Name = "小叶",  Course = "C"},
                new Student{ ID = 207, Name = "小苏",  Course = "C#"},
                new Student{ ID = 208, Name = "小梁",  Course = "Delphi"}
            };

            var result = stus.GroupBy(s => s.Course, (gKey, gItems) 
                  => (GroupKey: gKey, ItemCount: gItems.Count(), Items: gItems));

            Console.WriteLine("学员参与课程汇总:");
            StringBuilder strbuilder = new StringBuilder();
            foreach(var g in result)
            {
                strbuilder.AppendFormat("课程:{0}\n", g.GroupKey);
                strbuilder.AppendFormat("  参与人数:{0}\n", g.ItemCount);
                strbuilder.AppendLine("  名单:");
                foreach (Student s in g.Items)
                {
                    strbuilder.AppendFormat("    {0} - {1}\n", s.ID, s.Name);
                }
            }
            Console.WriteLine(strbuilder);
            Console.Read();
        }
    }

    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Course { get; set; }
    }

结果:

    学员参与课程汇总:
    课程:C
      参与人数:3
      名单:
        201 - 小王
        205 - 小郑
        206 - 小叶
    课程:C++
      参与人数:2
      名单:
        202 - 小曾
        203 - 小吕
    课程:C#
      参与人数:2
      名单:
        204 - 小孙
        207 - 小苏
    课程:Delphi
      参与人数:1
      名单:
        208 - 小梁

分组并取出最大数

    List<People> peoples = new List<People>
    {
        new People {Id=1,Name="张三",Amount =12 },
        new People {Id=2,Name="张三",Amount =13 },
        new People {Id=3,Name="李四",Amount =10 },
        new People {Id=4,Name="李四",Amount =11 }
    };
    // 方法一
    var list1 = peoples.OrderByDescending(q => q.Amount).GroupBy(q => q.Name).Select(q => q.First()).ToList();
    // 方法二
    var list2 = peoples.GroupBy(q => q.Name).Select(q => q.First(g => g.Amount == q.Max(a => a.Amount))).ToList();
posted @ 2020-12-14 14:09  一纸年华  阅读(7)  评论(0编辑  收藏  举报  来源