Linq不经常使用但必须记住的用法
2012-03-06 10:19
513 查看
经常在项目中为了完成某个特定的功能需要去写很多代码,后来走读书籍,才发现真的不必如此,所遇到的,微软已经为你做了,除了那些常用的linq语法外,掌握下面这些不是特别常用的,完成工作将变得更加得心应手:
条目:
1.匿名方法
2.OfType用法
3.AsEnumerable用法
4.柯里化(Currying)表达式
5.匿名类型的相等性
6.次要排序
7.Intersect扩展方法
8.Union扩展方法
9.Except扩展方法
10.Group多字段分组
11.实现左外联接
测试用例:
条目:
1.匿名方法
2.OfType用法
3.AsEnumerable用法
4.柯里化(Currying)表达式
5.匿名类型的相等性
6.次要排序
7.Intersect扩展方法
8.Union扩展方法
9.Except扩展方法
10.Group多字段分组
11.实现左外联接
测试用例:
class Program { static void Main(string[] args) { string split = new String('-', 30); Console.WriteLine("匿名方法"); Console.WriteLine(split); Func<int, int, int> func = delegate(int a, int b) { return a + b; }; Console.WriteLine(func(1, 2)); Console.WriteLine(""); Console.WriteLine("OfType用法"); Console.WriteLine(split); var arr = new object[] { 1232, "1231", 1231, "asdac" }; foreach (var value in arr.OfType<string>()) { Console.WriteLine(value); } Console.WriteLine(""); Console.WriteLine("AsEnumerable用法"); Console.WriteLine(split); Console.WriteLine("如果某个集合对象本身实现了Where方法,则可以obj.AsEnumerable().Where()方式进行调用,在某些场合下,这非常有用!"); Console.WriteLine(""); Console.WriteLine("ToLookup用法"); Console.WriteLine(split); var lookups = (new[]{ new {id=1,name="name1"}, new {id=1,name="name2"}, new {id=2,name="name3"}, }).ToLookup(o => o.id, o => o.name); foreach (var lookup in lookups) { Console.WriteLine("\b\b" + lookup.Key); foreach (var value in lookup) Console.WriteLine(value); } Console.WriteLine(""); Console.WriteLine("柯里化(Currying)表达式"); Console.WriteLine(split); Func<int, Func<int, int>> curry1 = x => y => x + y; Console.WriteLine(curry1(3)(4)); Console.WriteLine("理解:x=>y=>x+y 即在第一个匿名函数里面再封装一个匿名函数,在第二个匿名函数里,x可理解为闭包"); Console.WriteLine(""); Console.WriteLine("匿名类型的相等性"); Console.WriteLine(split); var eq1 = new { id = 1, name = "name" }; var eq2 = new { id = 1, name = "name" }; var eq3 = new { name = "name", id = 1 }; Console.WriteLine("eq1.Equals(eq2)=" + eq1.Equals(eq2)); Console.WriteLine("eq1.Equals(eq3)=" + eq1.Equals(eq3)); Console.WriteLine("eq1 == eq2 =" + (eq1 == eq2)); Console.WriteLine(""); Console.WriteLine("次要排序"); Console.WriteLine(split); var orderObj = new[]{ new {lastname="Kimmel",first="Paul",age=41}, new {lastname="Swanson",first="Dena",age=26}, new {lastname="Swanson",first="Joe",age=4}, new {lastname="Kimmel",first="Noah",age=11} }; var sordid = from order in orderObj orderby order.lastname, order.age select order; foreach (var o in sordid) { Console.WriteLine("lastname:{0},first:{1},age:{2}", o.lastname, o.first, o.age); } Console.WriteLine(""); Console.WriteLine("Intersect扩展方法"); Console.WriteLine(split); var intersect1 = new int[] { 1, 2, 32, 3, 4, 5, 65 }; var intersect2 = new int[] { 1, 2, 3, 345, 6, 5, 65 }; Array.ForEach<int>(intersect1.Intersect(intersect2).ToArray(), e => Console.WriteLine(e + ",")); Console.WriteLine(""); Console.WriteLine("Union扩展方法"); Console.WriteLine(split); var union1 = new int[] { 1, 2, 32, 3, 4, 5, 65 }; var union2 = new int[] { 1, 2, 3, 345, 6, 5, 65 }; Array.ForEach<int>(union1.Union(union2).ToArray(), e => Console.WriteLine(e + ",")); Console.WriteLine(""); Console.WriteLine("Except扩展方法"); Console.WriteLine(split); var except1 = new int[] { 1, 2, 32, 3, 4, 5, 65 }; var except2 = new int[] { 1, 2, 3, 345, 6, 5, 65 }; Array.ForEach<int>(except1.Except(except2).ToArray(), e => Console.WriteLine(e + ",")); Console.WriteLine(""); Console.WriteLine("Group多字段分组"); Console.WriteLine(split); var groups = (new[]{ new {name="name1",age=18}, new {name="name2",age=18}, new {name="name4",age=19} }).GroupBy(o=>new{o.age,o.name}); foreach (var group in groups) { Console.WriteLine("key.age:{0},key.name:{1},count:{2}", group.Key.age, group.Key.name, group.Count()); } Console.WriteLine(""); Console.WriteLine("实现左外联接"); Console.WriteLine(split); var customers = new Customer[]{ new Customer(){ CompanyName="company1", ID=1}, new Customer(){ CompanyName="company2", ID=2}, new Customer(){ CompanyName="company3", ID=3}, new Customer(){ CompanyName="company4", ID=4}, new Customer(){ CompanyName="company5", ID=5}, }; var orders = new Order[]{ new Order(){ CompanyId=1, ID=1, ItemDescription="this is order 1"}, new Order(){ CompanyId=1,ID =2,ItemDescription="this is order 2"} }; var joinGroup = from customer in customers join order in orders on customer.ID equals order.CompanyId into orderCollection from item in orderCollection.DefaultIfEmpty(new Order()) select new { customer, item }; foreach (var g in joinGroup) { Console.WriteLine("companyname:{0},companyid:{1},itemdescription:{2}", g.customer.CompanyName, g.customer.ID, g.item.ItemDescription); } 4000 Console.Read(); } public class Customer { public int ID; public string CompanyName; } public class Order { public int ID; public int CompanyId; public string ItemDescription; } }
相关文章推荐
- Linq不经常使用但必须记住的用法(一)
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法-------wince里面的中断IST经常使用
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法-------wince里面的中断IST经常使用。
- Java经常使用类及其经常用法
- 在使用vue脚手架之前你必须掌握的:vue的模版以及路由用法
- 使用C语言之前必须记住的几个关键
- asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法-------wince里面的中断IST经常使用
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法-------wince里面的中断IST经常使用。 .
- Dataset用法实践之三 dataset与LINQ使用
- LINQ entityset 不包含GetEnumerator的定义,在MVC中使用entity framework(EF)出现“必须添加对程序集“System.Data.Entity”解决方法
- 使用ClientDataSet 时必须记住的几点
- asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法-------wince里面的中断IST经常使用。
- android中handler简单用法,之前只是复制粘贴的使用,不明白原理,现在补上!
- try-catch的使用(简单用法 )
- 错误“操作必须使用一个可更新的查询”原因及解决办法
- LINQ to SQL语句(12)之Delete和使用Attach
- 新闻模块之图片新闻详细内容-使用datalist 模块获取图片新闻数据-页面表示层(Linq方式)-4
- 新的应用必须使用iOS8 SDK生成,而且必须支持64位