Linq to Objects
澹台展鹏
2023-12-01
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace xStudyezLinq { public class UserInfo { public int ID { get; set; } public string UserName { get; set; } public List<string> AliasName { get; set; } public string Email { get; set; } public int RoleId { get; set; } } public class RoleInfo { public int RoleId { get; set; } public string RoleName { get; set; } } public class LinqSelect { public static void Query() { string[] strs = new string[] { "2008", "08", "099", "20" }; var result = from str in strs where str.Length > 2 orderby str select str; foreach (var i in result) { Console.WriteLine(i.PadLeft(10, '0')); } } public static void ObjectInit() { UserInfo u = new UserInfo { ID = 2008, UserName = "dhzstudyez", AliasName = new List<string>() { "ASP.NET" }, Email = "d.h.z@live.cn", RoleId = 2 }; List<UserInfo> users = new List<UserInfo>(); for (int i = 1; i < 10; i++) { users.Add( new UserInfo() { ID = i, RoleId = i, Email = "user" + i.ToString() + "@asp.net", AliasName = new List<string>() { "asp.net" }, UserName = "userName_" + i.ToString() }); } var value = from c in users where c.ID < 3 select new { UserName = c.UserName, RolleId = c.RoleId }; foreach (var x in value) { Console.WriteLine(x.UserName); } } public static void LambdaQuery() { int[] ints = new int[100]; for (int i = 0; i < 100; i++) ints[i] = i; var values = ints.Select(i => i % 5); var result = values.Distinct(); //foreach (var v in values) //{ // Console.WriteLine(v); //} foreach (var r in result) { Console.WriteLine(r); } } public static void QueryExpressionBase() { List<UserInfo> users = new List<UserInfo>(); for (int i = 1; i < 10; i++) { users.Add( new UserInfo() { RoleId = 2, UserName = string.Format("User_{0}", i), ID = i, AliasName = new List<string>() { "Aspnet_User", "User" + i.ToString() }, Email = string.Format("Ui{0}@studyez.cn", i) }); } var value = from u in users from an in u.AliasName where u.ID < 3 && an.ToString().IndexOf("1") > -1 select u; foreach (var v in value) { Console.WriteLine(v.UserName); } } static bool exist(string userName) { return userName.IndexOf("1") > -1; } public static void QueryExpressionBase2() { List<UserInfo> users = new List<UserInfo>(); for (int i = 1; i < 10; i++) { users.Add( new UserInfo() { RoleId = 2, UserName = string.Format("aUser_{0}", i), ID = i, AliasName = new List<string>() { "aAspnet_User", "aUser" + i.ToString() }, Email = string.Format("aUi{0}@studyez.cn", i) }); } List<UserInfo> bu = new List<UserInfo>(); for (int i = 1; i < 10; i++) { bu.Add( new UserInfo() { RoleId = 3, UserName = string.Format("bUser_{0}", i), ID = i, AliasName = new List<string>() { "bAspnet_User", "bUser" + i.ToString() }, Email = string.Format("bUi{0}@studyez.cn", i) }); } var value = from au in users where au.ID > 3 from b in bu where b.ID > 5 select new { UserName = au.UserName, UserMalie = b.Email }; foreach (var v in value) { Console.WriteLine(v.UserName); Console.WriteLine(v.UserMalie); } var ext = from au in users where au.ID > 3 && exist(au.UserName) select au; foreach (var e in ext) { Console.WriteLine(e.UserName.PadLeft(20, 'X')); } IEnumerable<int> c = from u in users select u.ID * 100; foreach (int ic in c) { Console.WriteLine(ic); } Console.WriteLine("Group by "); var grpValue = from u in users group u by u.ID % 2 == 0; foreach (var v in grpValue) { Console.WriteLine("-----------------------"); foreach (UserInfo ui in v) { Console.WriteLine(ui.ID); Console.WriteLine(ui.UserName); } } Console.WriteLine("Group To"); var grpValue2 = from u in users group u by u.ID % 2 == 0 into g where g.Count() > 4 select g; foreach (var v in grpValue2) { Console.WriteLine("-----------------------"); foreach (UserInfo ui in v) { Console.WriteLine(ui.ID); Console.WriteLine(ui.UserName); } } } public static void JoinQuery() { List<UserInfo> users = new List<UserInfo>(); for (int i = 1; i < 10; i++) { users.Add( new UserInfo() { RoleId = i % 2, UserName = string.Format("aUser_{0}", i), ID = i, AliasName = new List<string>() { "aAspnet_User", "aUser" + i.ToString() }, Email = string.Format("aUi{0}@studyez.cn", i) }); } List<RoleInfo> roles = new List<RoleInfo>() { new RoleInfo() { RoleId = 0, RoleName = "Admin" }, new RoleInfo(){ RoleName ="Aspnet", RoleId =1 } }; var value = from u in users where u.ID < 9 join r in roles on u.RoleId equals r.RoleId select new { UserName = u.UserName, RoleName = r.RoleName }; Console.WriteLine("InnerJoin"); foreach (var x in value) { Console.WriteLine(string.Format("{0} {1}", x.UserName, x.RoleName)); } Console.WriteLine("GroupJoin"); var value2 = from u in users where u.ID < 9 join r in roles on u.RoleId equals r.RoleId into g select new { UserName = u.UserName, Roles=g.ToList () }; foreach (var x in value2) { Console.WriteLine(string.Format("{0} {1}", x.UserName, x.Roles.Count)); } } } public class LinqOperation { List<UserInfo> users; List<RoleInfo> roles; public LinqOperation() { users = new List<UserInfo>(); for (int i = 1; i < 100; i++) { users.Add( new UserInfo() { RoleId = i % 2, UserName = string.Format("User_{0}", i), ID = i, AliasName = new List<string>() { "Aspnet_User", "User" + i.ToString() }, Email = string.Format("Ui{0}@studyez.cn", i) }); } roles = new List<RoleInfo>() { new RoleInfo() { RoleId = 0, RoleName = "Admin" }, new RoleInfo(){ RoleName ="Aspnet", RoleId =1 } }; } public void WhereFilterQuery() { var values = from u in users where u.ID < 3 select u; var vOther = users.Where(u => u.UserName.IndexOf("1") != -1); foreach (var v in values) Console.WriteLine(v.UserName); foreach (var o in vOther) Console.WriteLine(o.UserName); } public void SelectManyQuery() { int[] ione = { 1, 2, 3 }; int[] tow = { 4, 5, 6 }; List<int[]> lst =new List<int[]> { ione, tow }; var v = lst.SelectMany(i => i); foreach (var vi in v) Console.WriteLine(vi); } public void SelectTop() { Func<UserInfo, bool> del = x => x.ID > 10; var us = users.TakeWhile((x, i) => x.ID < 10); foreach (var ui in us) { Console.WriteLine(ui.ID); } ///TakeWhile會一直取出元素,直到某個元素不符合所指定的 Lambda Expression為止, ///這意味著假如指定ID>10 時,將不會有任何元素列出, ///因為陣列中的第一個元素就已經不符合條件了,所以While動作就結束了。 var us2 = users.TakeWhile((x, i) => x.ID > 10); foreach (var u2 in us2) { Console.WriteLine(u2.ID); } /*等价的方式: List<UserInfo> ls=new List<UserInfo>(); i=0; while(users[i].ID>10){ ls.Add(x); i++; } return ls; */ var xt = users.Max(u => u.ID); Console.WriteLine(xt); var u3 = users.Skip(2).Take(10); foreach (var x3 in u3) Console.WriteLine(x3.ID); Console.WriteLine("SkipWhile"); var u4 = users.SkipWhile((x, i) => x.ID < 10); foreach (var x4 in u4) Console.WriteLine(x4.ID); Console.WriteLine("SkipWhile2"); ///若指定ID>=10 時,那麼將會列出所有元素,因為在第一個元素時,While就已經結束了 var u5 = users.SkipWhile((x, i) => x.ID >= 10); /* List<UserInfo> ls = new List<UserInfo>(); i = 0; while (users[i].ID >= 10) { i++; } for(;i<100;i++) ls.add(users[i]); return ls; */ foreach (var x5 in u5) Console.WriteLine(x5.ID); } public void JoinQuery() { var value = users.Join(roles, user => user.RoleId, role => role.RoleId, (user, role) => new { UserName = user.UserName, RoleName = role.RoleName }); foreach (var v in value) Console.WriteLine("{0}:{1}", v.UserName, v.RoleName); } } } ---------------------------代码纯是用于自学。