linq复习 向右看齐 2022-08-11 00:56 200阅读 0赞 /// <summary> /// join多对多链接 /// </summary> public void Test6() { var namelist = new[] { new{id = "1", name = "Jon"}, new{id = "2", name = "Holly"}, new{id = "1", name = "Tom"}, new{id = "3", name = "Tom"}, new{id = "3", name = "Tom"} }; var dtlist = new[] { new{id = "2", age = 23, }, new{id = "2", age = 28, }, new{id = "3", age = 26}, new{id = "1", age = 25} }; var query = from n in namelist join d in dtlist on n.id equals d.id into tempdt select new { }; //... } /// <summary> /// 自联接关系 /// </summary> public void Test8() { //会先做笛卡尔积,然后再筛选,比较消耗 //var q = from e1 in db.Employees // from e2 in e1.Employees // where e1.City == e2.City // select new // { // name1 = e1.FirstName + e1.LastName, // name2 = e2.FirstName + e2.LastName, // e1.City // }; } /// <summary> /// 一对多=> 双向联接(Two way join) /// </summary> /// public void Test9() { //var q = from c in db.Customers // join o in db.Orders on c.CustomerID // equals o.CustomerID into orders // select new // { // c.ContactName, // OrderCount = orders.Count() // }; /*在一对多关系中,左边是1,它每条记录为c(from c in db.Customers), * 右边是Many,其每条记录叫做o ( join o in db.Orders ), * 每对应左边的一个c,就会有一组o,那这一组o,就叫做orders,也就是说, * 我们把一组o命名为orders,这就是into用途。这也就是为什么在select语句中,orders可以调用聚合函数Count。 */ } /// <summary> /// join 一对一链接 /// </summary> public void Test5() { var str = new[] { new{id = "1", name = "Jon"}, new{id = "2", name = "Holly"}, new{id = "3", name = "Tom"} }; var str2 = new[] { new{id = "2", age = 23}, new{id = "3", age = 26}, new{id = "1", age = 25} }; var query = from n in str join a in str2 on n.id equals a.id into tempa from ta in tempa.DefaultIfEmpty() select new { n, ta }; foreach (var item in query) { Response.Write(string.Format("name:{0}, age:{1}<br />", item.n.name, item.ta.age.ToString())); /* name:Jon, age:25 name:Holly, age:23 name:Tom, age:26 */ } //每个类别中单价最低的产品 //var categories = from p in db.Products // group p by p.CategoryID into g // select new // { // CategoryID = g.Key, // CheapestProducts = from p2 in g // where p2.UnitPrice == g.Min(x => x.UnitPrice) // //每个类别中单价高于该类别平均单价的产品 // //where p2.UnitPrice == g.Average(x = x.UnitPrice) // select p2 // }; } /// <summary> /// join、一对多链接 /// </summary> public void Test4() { //定义匿名类型 // var str = new { id = "1", name = "jon" }; //定义匿名类型数组 var str = new[] { new{id = "1", name = "Jon"}, new{id = "2", name = "Holly"}, new{id = "3", name = "Tom"} }; var str2 = new[] { new{id = "1", name = "Robin"}, new{id = "2", name = "William"}, new{id = "2", name = "kkk"}, new{id = "3", name = "jjjj"} }; //一对多情况 //var query = from a in str // join b in str2 on a.id equals b.id // select new { Aname = a.name, Bname = b.name }; //foreach (var item in query) //{ // Response.Write(string.Format("Aname:{0}, Bname:{1}", item.Aname, item.Bname) + "<br />"); // /* // Aname:Jon, Bname:Robin // Aname:Holly, Bname:William // Aname:Holly, Bname:kkk // Aname:Tom, Bname:jjjj // */ //} var query = from a in str join b in str2 on a.id equals b.id into temp2 select new { a, temp2 }; foreach (var item in query) { Response.Write(string.Format("Aname:{0}, =>Bname Count:{1}", item.a.name, item.temp2.Count()) + "<br />"); /* Aname:Jon, =>Bname Count:1 Aname:Holly, =>Bname Count:2 Aname:Tom, =>Bname Count:1 */ } } /// <summary> /// group by测试 /// </summary> public void Test3() { char[] gitem = { 'a', 'b', 'c', 'f' }; //首字母分组对象集合 string[] str = { "aa", "bb", "cc", "dd", "fine" }; //var query = from s in str // group s by s[0] into ws //针对范围变量s,用s[0]分组 // where gitem.Contains(ws.Key) // orderby ws.Key ascending // select ws.Key; var q = from s in str where gitem.Contains(s[0]) select s; //foreach (var item in query) foreach (var item in q) { //Response.Write(string.Format("{0},<br />", item)); /* a, b, c, f, */ Response.Write(string.Format("{0},<br />", item)); } } /// <summary> /// let测试 /// </summary> public void Test2() { string[] strings = { "A penny saved is a penny earned.", "The aaxly sdj", "the pa is no" }; //选出单词首字母以a和e开头的,不区分大小写 var g = from j in strings let w = j.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) from k in w let nw = k.ToLower() where nw[0] == 'a' || nw[0] == 'e' select k; foreach (var item in g) { Response.Write(string.Format("{0},<br />", item)); } } /// <summary> /// 自链接 /// </summary> public void Test1() { List<Student> students = new List<Student> { new Student { LastName="xiaogui",Scores=new List<int>{97,42,91,60} }, new Student { LastName="xiaozhan",Scores=new List<int>{50,92,81,60} }, new Student { LastName="xiaolan",Scores=new List<int>{32,32,81,90} }, new Student { LastName="xiaowan",Scores=new List<int>{92,22,81,60} }, }; //选出分数中大于90的学生 var q = from s in students from o in s.Scores where o > 90 select new { lastname = s.LastName, o }; foreach (var item in q) { Response.Write(string.Format("{0}:{1}<br />", item.lastname, item.o)); /*Note:xiaogui出现了2次 * xiaogui:97 xiaogui:91 xiaozhan:92 xiaowan:92 */ } } }//testlinq public class Student { public string LastName { get; set; } public List<int> Scores { get; set; } } public class Employee { public string First { get; set; } public string Last { get; set; } public int ID { get; set; } }
相关 Linq分组 Linq应用场景 linq的语法通过System.Linq下面的Enumerable类提供支持,也就是说,只要是实现了IEnumerable<T>的对象都可以使用Linq的语 水深无声/ 2024年03月29日 16:18/ 0 赞/ 85 阅读
相关 LINQ 简介 LINQ(Language Integrated Query,语言集成查询)在C\编程语言中集成了查询语法,可以用相同的语法访问不同的数据源。LINQ提供了不同数据 不念不忘少年蓝@/ 2023年02月21日 14:08/ 0 赞/ 8 阅读
相关 LINQ LINQ(语言集成查询)是对C\语言的扩展,允许C\代码以查询数据库的方式操作内存数据。 一些关键字: from:指定范围变量和数据源 where:根据条件从数据源 爱被打了一巴掌/ 2022年09月17日 07:29/ 0 赞/ 231 阅读
相关 Linq应用 在说LINQ之前必须先说说几个重要的C\语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源 在隐式类型出现之前, 我们 ゞ 浴缸里的玫瑰/ 2022年05月24日 22:14/ 0 赞/ 202 阅读
相关 Linq-GroupBy 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g ╰半橙微兮°/ 2022年05月19日 12:21/ 0 赞/ 262 阅读
相关 linq 什么是linq: Language Integrated Query 语言集成查询,,是进行数据访问的编程模式,可以通过延迟查询,降低访问数据的复杂度。 那么它是怎么 ╰+攻爆jí腚メ/ 2022年03月19日 07:54/ 0 赞/ 288 阅读
相关 linq(1)常见查询(linq表达式) 本文主要讲解的是linq的查询用法。 采用的数据 //定义数组man var man = new[] 绝地灬酷狼/ 2022年01月23日 07:55/ 0 赞/ 415 阅读
相关 LINQ之路(1):LINQ基础 本文将从什么是LINQ(What)、为什么使用LINQ(Why)以及如何使用LINQ(How)三个方面来进行说明。 1.什么是LINQ LINQ(Language In 我会带着你远行/ 2021年11月26日 07:40/ 0 赞/ 557 阅读
还没有评论,来说两句吧...