whereif扩展
2013-10-24 13:02
330 查看
三、WhereIf 完整代码
namespace System.Linq { public static class WhereIfExtension { public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate, bool condition) { return condition ? source.Where(predicate) : source; } public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, Expression<Func<T, int, bool>> predicate, bool condition) { return condition ? source.Where(predicate) : source; } public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, Func<T, bool> predicate, bool condition) { return condition ? source.Where(predicate) : source; } public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, Func<T, int, bool> predicate, bool condition) { return condition ? source.Where(predicate) : source; } } }
public IQueryable<Person> Query(IQueryable<Person> source, string name, string code, string address) { return source .WhereIf(p => p.Name.Contains(name), string.IsNullOrEmpty(name) == false) .WhereIf(p => p.Code.Contains(code), string.IsNullOrEmpty(code) == false) .WhereIf(p => p.Code.Contains(address), string.IsNullOrEmpty(address) == false); }
相关文章推荐
- PeopleSoft Object Types Definitions
- c++中,引用和指针的区别
- 解决 Iis7中的“ISAPI 和 CGI 限制”错误
- MFC对话框程序按回车后程序退出的解决办法
- d-ary heaps 多叉树堆排序C++实现
- GlobeMapper生成Google瓦片
- Dreamweaver vs phpStorm
- Android 获取屏幕尺寸与密度
- 转:: 刺鸟:用python来开发webgame服务端(5)
- Senior Software Engineer - 2013/10/21
- HDU 1412 {A} + {B} (STL)
- 括号匹配I
- Hadoop Job在集群上运行出现“Error initializing”错误
- 市交通运输局召开专题会议积极征求“五区一港两基地”公交线路调整意见
- MATLAB7 MYSQL数据库
- A Byte of Python实例中的备份数据小程序第五版
- 关于copy, mutableCopy, 浅拷贝,深拷贝
- 输出二进制数
- Pro Android学习笔记(四二):Fragment(7):切换效果
- android开发在adapter中使用反射添加元素