博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#Linq
阅读量:6708 次
发布时间:2019-06-25

本文共 4118 字,大约阅读时间需要 13 分钟。

Main
1 static void Main(string[] args)2         {3             BasicConcept();4 5             QuerySyntax();6 7             QueryOperations();8             Console.ReadLine();9         }
View Code

1、Query Syntax、Method Syntax

1 private static void BasicConcept() 2         { 3             int[] numbers = { 5, 10, 8, 3, 6, 12 }; 4             //1、Query syntax 5             var numQuery1 = from num in numbers 6                             where num % 2 == 0 7                             orderby num 8                             select num; 9             foreach (var i in numQuery1)10             {11                 Console.Write(i + " ");12             }13             Console.WriteLine();14             //2、Method syntax15             var numQuery2 = numbers.Where(p => p % 2 == 0).OrderBy(p => p);16             foreach (var i in numQuery2)17             {18                 Console.Write(i + " ");19             }20         }
View Code

2、Query Syntax语法

1 private static void QuerySyntax() 2         { 3             //1、Data Source 4             int[] numbers = { 0, 1, 2, 3, 4, 5, 6 }; 5  6             //2、Query creation 7             var numQuery = from num in numbers 8                            where num % 2 == 0 9                            select num;10 11             //3、Query execution12             foreach (var num in numQuery)13             {14                 Console.Write("{0,1} ", num);15             }16         }
View Code

3、关键字group by、join、let、into

1 private static void QueryOperations() 2         { 3             List
custorms = new List
(); 4 custorms.Add(new Customer() { Name = "Jack", City = "Beijing" }); 5 custorms.Add(new Customer() { Name = "Joy", City = "Beijing" }); 6 custorms.Add(new Customer() { Name = "Tom", City = "Shanghai" }); 7 8 List
employees = new List
(); 9 employees.Add(new Employee() { Name = "Jack", ID = 101 });10 employees.Add(new Employee() { Name = "Jackson", ID = 102 });11 12 var query = from c in custorms13 group c by c.City into cusGroup14 where cusGroup.Count() >= 215 select new { City = cusGroup.Key, Number = cusGroup.Count() };16 17 //foreach (var cg in query)18 //{19 // Console.WriteLine(cg.Key);20 // foreach (var c in cg)21 // {22 // Console.WriteLine(" {0}", c.Name);23 // }24 //}25 26 foreach (var c in query)27 {28 Console.WriteLine("{0} Count {1}", c.City, c.Number);29 }30 31 var queryJoin = from c in custorms32 join e in employees on c.Name equals e.Name33 select new { PersonName = c.Name, PersonID = e.ID, PersonCity = c.City };34 35 foreach (var p in queryJoin)36 {37 Console.WriteLine("{0} {1} {2}", p.PersonName, p.PersonID, p.PersonCity);38 }39 40 string[] strings = { "Hello World", "This is Friday", "Are you happy?" };41 var stringQuery = from s in strings42 let words = s.Split(' ')43 from word in words44 let w = word.ToUpper()45 select w;46 foreach (var s in stringQuery)47 {48 Console.Write(s + " ");49 }50 }51 class Customer52 {53 public string Name54 {55 get;56 set;57 }58 public string City59 {60 get;61 set;62 }63 }64 65 class Employee66 {67 public string Name { get; set; }68 public int ID { get; set; }69 }
View Code

 

转载于:https://www.cnblogs.com/SharonHwang/p/5334735.html

你可能感兴趣的文章
mvc中的ViewData用到webfrom中去
查看>>
小白学数据分析------>描述性统计术语汇总
查看>>
[转载]java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法
查看>>
SKY IM-A800S 驱动下载
查看>>
应用程序 数据缓存
查看>>
TFS签入签出
查看>>
第二条:遇到多个构造器参数(Constructor Parameters)时要考虑用构建器(Builder)
查看>>
成长,没你想象的那么迫切
查看>>
ASP.NET Core 中文文档 第一章 入门
查看>>
jQuery入门(2)使用jQuery操作元素的属性与样式
查看>>
贴片电阻分类、阻值、功率、封装、尺寸
查看>>
Mqtt协议IOS端移植2
查看>>
【Eclipse】eclipse中设置tomcat启动时候的JVM参数
查看>>
10.查看npm安装信息和版本号
查看>>
国际化环境下系统架构演化
查看>>
C#跟着阿笨玩一起玩异步Task实战(一)
查看>>
Sqoop-1.4.6安装部署及详细使用介绍
查看>>
oracle 存储过程 示例
查看>>
正态分布与中心极限定理
查看>>
cf1027F. Session in BSU(并查集 匈牙利)
查看>>