您的位置:首页 > 其它

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);
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: