.net MVC 随笔4
来源:互联网 发布:童装淘宝店简介怎么写 编辑:程序博客网 时间:2024/05/18 02:27
函数调用方式
- 方式1.
public static decimal TotalPrices(this IEnumerable<Product> products) { decimal total = 0; foreach (Product prod in products ) { total += prod?.Price ?? 0; } return total; }public static IEnumerable<Product> FilterByPrice(this IEnumerable<Product> productEnum,decimal minimumPrice) { foreach (Product prod in productEnum) { if((prod?.Price??0)>=minimumPrice)//price !=0&&>=minimumPrice { yield return prod; } } } public static IEnumerable<Product> FilterByName(this IEnumerable<Product> productEnum,char firstLetter) { foreach(Product prod in productEnum) { if(prod?.Name?[0]==firstLetter) { yield return prod; } } }
//这里调用了两个函数,一个是判断消费大于20,一个是累加集合。decimal arrayTotal = productArray.FilterByPrice(20).TotalPrices();//这里调用了两个函数,一个是判读首字母是否为S,一个是累加集合。decimal nameFilterTotal = productArray.FilterByName('S').TotalPrices();
- 方法2
// this 是prodcut的前置接口,放在.前面 public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum,Func<Product,bool>selector) { foreach(Product prod in productEnum) { if(selector(prod)) { yield return prod; } } }// 判断函数 价格》=20,返回值bool类型 bool FilterByPrice(Product p) { return (p?.Price ?? 0) >= 20; } //这里调用了两个函数,一个filer,读取filerbyprice的BOOL值,一个是累加集合。 decimal priceFilterTotal = productArray.Filter(FilterByPrice).TotalPrices();//判断函数返回值bool,首字母是SFunc<Product, bool> nameFilter = delegate (Product prod) { return prod?.Name[0] == 'S'; }; //这里调用了两个函数,一个filer,读取namefiler的BOOL值,一个是累加集合。 decimal nameFilterTotal = productArray.Filter(nameFilter).TotalPrices();
- 方法3
//另一种用法decimal priceFilterTotal = productArray.Filter(p => (p?.Price ?? 0) >= 20).TotalPrices();decimal nameFilterTotal = productArray.Filter(p =>p?.Name?[0]== 'S').TotalPrices();
三种方法实现的效果一样,第一种最易懂,第三种最简洁(难读)。
阅读全文
0 0
- .net MVC 随笔4
- 知识点随笔.net(mvc)
- .net MVC 学习随笔
- .net MVC 随笔2
- .net MVC 随笔3
- .net MVC 随笔5
- .net MVC随笔6
- EXT.NET MVC 5 工作随笔01
- EXT.NET MVC 5 工作随笔03
- .net 随笔(4)
- spring mvc随笔
- Spring mvc随笔
- MVC ListView 随笔
- 学习MVC随笔
- Spring MVC 使用随笔
- ASP.NET MVC(4)
- ASP.NET MVC(4)
- 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架开发随笔一
- 关于elasticsearch属性not_analyzed,坑
- TextView属性大全
- Integrate cache的易错点
- java测试工具
- java 类、对象(实例)
- .net MVC 随笔4
- 20171108Link
- Hive 之 内部表和外部表区别
- is和==的区别
- 游戏场景概念设计教程之构图技巧讲解
- POJ 2151.Check the difficulty of problems
- 自定义动态跳动的三个加载小点
- 关于Libevent的快速可移植非阻塞网络编程(block-》select-》poll-》epoll-》libevent)
- VS2015 tfs的使用教程