欢迎来到忆约科技! 手机版

C# 社区推荐

当前位置:首页 > 编程专栏 > C# > 正文

C# 操作Linq, 查找, 获取, 合并, 排序

编辑时间:2020年08月07日 05:00 作者:E小编

C# 操作Linq, 查找, 获取, 合并, 排序
using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Emplayee> emplayeeList = GetEmplayeeList();  // 初始化人员列表

            // 获取人员Id数组  
            // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            Guid[] arr = emplayeeList.Select(a => a.Id).ToArray();

            // 获取指定人员信息
            var emplayee = (from a in emplayeeList where a.Id == new Guid("{875d217d-840f-470d-be03-8fb3a814d9c6}") select a).FirstOrDefault();

            // 获取性别女的人员
            var emplayee2 = (from a in emplayeeList where a.Sex == "女" select a).FirstOrDefault();

            // 获取所有性别
            var emplayee2_1 = (from a in emplayeeList where (a.Sex == "女" || a.Sex == "男") select a);

            // 获取性别并且按照年龄排序
            var emplayee2_2 = (from a in emplayeeList
                               orderby a.Age descending
                               where (a.Sex == "女" || a.Sex == "男")
                               select a);

            // 获取性别并且按照年龄排序,输出第一行
            var emplayee2_3 = (from a in emplayeeList
                               orderby a.Age descending
                               where (a.Sex == "女" || a.Sex == "男")
                               select a).FirstOrDefault();

            // 按照性别进行分组合并,Id进行逗号合并输出
            var emplayee3 = from a in emplayeeList
                            group a by a.Sex into g
                            let idArray = g.Select(b => b.Id.ToString()).ToArray()
                            select new { id = String.Join(",", idArray), name = g.Key };// 输出id, name字段

            // 按照性别进行分组合并,Id进行逗号合并输出,统计 男 女 各有多少人
            var emplayee4 = from a in emplayeeList
                            group a by a.Sex into g
                            let idArray = g.Select(b => b.Id.ToString()).ToArray()
                            select new { id = String.Join(",", idArray), name = g.Key, count = g.Count() };// 输出id, name, count字段

            // 按照性别进行分组合并,Id进行逗号合并输出,统计 男 女 各有多少人,分别计算男女的总年龄
            var emplayee5 = from a in emplayeeList
                            group a by a.Sex into g
                            let idArray = g.Select(b => b.Id.ToString()).ToArray()
                            select new { id = String.Join(",", idArray), name = g.Key, count = g.Count(), sumage = g.Sum(a => a.Age) };// 输出id, name, count,sumage字段

            // 按照性别进行分组合并,Id进行逗号合并输出,统计 男 女 各有多少人,分别计算男女的总年龄,按照某个字段进行排序
            var emplayee6 = from a in emplayeeList
                            group a by a.Sex into g
                            orderby g.Count() descending
                            let idArray = g.Select(b => b.Id.ToString()).ToArray()
                            select new { id = String.Join(",", idArray), count = g.Count(), test = g.First().Sex, sumage = g.Sum(a => a.Age) };// 输出id, count,sumage字段

            // 查找包含的元素
            string[] names = new string[] { "张小强" };
            var emplayee7 = from a in emplayeeList
                            where names.Contains(a.Name)
                            select a;


            // Linq分页查询
            // skip是在分页在前有多少条数据,也就是在当前页数据之前的数据总和,(跳过序列中指定数量的元素,然后返回剩余的元素) pageSize*(pageIndex-1)
            // Take 是返回多少条数据,也就是pageSize!
            int pageSize = 2;// 每页显示多少条
            int pageIndex = 2;// 当前是第几页
            List<Emplayee> emplayee8 = emplayeeList.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
        }

        /// <summary>
        /// 返回姓名集合
        /// </summary>
        /// <returns></returns>
        public static string GetNameItems(List<Emplayee> list)
        {
            string[] items = list.Select(a => a.Name).ToArray();
            return string.Join(",", items);
        }

        /// <summary>    
        /// 初始化人员列表    
        /// </summary>    
        /// <returns></returns>    
        public static List<Emplayee> GetEmplayeeList()
        {
            List<Emplayee> emplayees = new List<Emplayee>();
            emplayees.Add(new Emplayee() { Sn = 1, Id = new Guid("{875d217d-840f-470d-be03-8fb3a814d9c6}"), Name = "张小强", Sex = "男", Age = 20 });
            emplayees.Add(new Emplayee() { Sn = 2, Id = new Guid("{ec08bde0-1be9-4eec-927f-e179d3a6b49a}"), Name = "王晓丽", Sex = "女", Age = 21 });
            emplayees.Add(new Emplayee() { Sn = 3, Id = new Guid("{c0961aee-d29f-49b2-8325-f2ee4e858f21}"), Name = "蔡淑芬", Sex = "女", Age = 21 });

            emplayees.Add(new Emplayee() { Sn = 5, Id = new Guid("{D51127AD-C003-4C5A-BDE9-B876794F0048}"), Name = "李梅", Sex = "女", Age = 25 });
            emplayees.Add(new Emplayee() { Sn = 4, Id = new Guid("{18EBB0A5-C6BF-4781-98A0-18BB1E50806D}"), Name = "李雷", Sex = "男", Age = 28 });
            emplayees.Add(new Emplayee() { Sn = 5, Id = new Guid("{88C0D6F3-2F4A-40AE-A6E8-D0039208291D}"), Name = "韩梅梅", Sex = "女", Age = 25 });

            return emplayees;
        }

        /// <summary>    
        /// 人员Model    
        /// </summary>    
        public class Emplayee
        {
            /// <summary>
            /// 序号
            /// </summary>
            public int Sn { get; set; }

            /// <summary>  
            /// 编号  
            /// </summary>  
            public Guid Id { get; set; }

            /// <summary>    
            /// 姓名    
            /// </summary>    
            public string Name { get; set; }

            /// <summary>    
            /// 性别    
            /// </summary>    
            public string Sex { get; set; }

            /// <summary>    
            /// 年龄    
            /// </summary>    
            public int Age { get; set; }
        }
    }
}


二维码