發(fā)布時間:2020-08-07 05:00 作者:獨孤劍 閱讀:1877
using System; using System.Collections.Generic; using System.Linq; namespace CSharpLinq { class Program { static void Main(string[] args) { List<Emplayee> emplayeeList = GetEmplayeeList(); // 初始化人員列表 // 獲取人員Id數(shù)組 // 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進行逗號合并輸出,統(tǒng)計 男 女 各有多少人 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進行逗號合并輸出,統(tǒng)計 男 女 各有多少人,分別計算男女的總年齡 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進行逗號合并輸出,統(tǒng)計 男 女 各有多少人,分別計算男女的總年齡,按照某個字段進行排序 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是在分頁在前有多少條數(shù)據(jù),也就是在當前頁數(shù)據(jù)之前的數(shù)據(jù)總和,(跳過序列中指定數(shù)量的元素,然后返回剩余的元素) pageSize*(pageIndex-1) // Take 是返回多少條數(shù)據(jù),也就是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; } } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞