#region IQueryable<T>的扩展方法

        #region 根据第三方条件是否为真是否执行指定条件的查询
/// <summary>
/// 根据第三方条件是否为真是否执行指定条件的查询
/// </summary>
/// <typeparam name="T">动态类型</typeparam>
/// <param name="source">要查询的数据源</param>
/// <param name="where">条件</param>
/// <param name="condition">第三方条件</param>
/// <returns>查询的结果</returns>
public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, Expression<Func<T, bool>> where,
bool condition)
{
if (PublicHelper.CheckArgument(where, "where"))
{
return condition ? source.Where(where) : source;
}
return null;
}
#endregion #region 把IQueryable<T>集合按照指定的属性与排序方式进行排序
/// <summary>
/// 把IQueryable集合按照指定的属性与排序方式进行排序
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="source">要排序的数据集</param>
/// <param name="propName">要排序的属性名称</param>
/// <param name="listSort">排序方式</param>
/// <returns>返回排序后的结果集</returns>
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propName,
ListSortDirection listSort = ListSortDirection.Ascending)
{
if (PublicHelper.CheckArgument(propName, "propName"))
{
return QueryableHelper<T>.OrderBy(source, propName, listSort);
}
return null;
}
#endregion #region 把IQueryable<T>集合按照指定的属性与排序方式进行排序
/// <summary>
/// 把IQueryable集合按照指定的属性与排序方式进行排序
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="source">要排序的数据集</param>
/// <param name="sortCondition">排序条件</param>
/// <returns>返回排序后的结果集</returns>
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, PropertySortCondition sortCondition)
{
if (PublicHelper.CheckArgument(sortCondition, "sortCondition"))
{
return source.OrderBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
}
return null;
}
#endregion #region 把IOrderedQueryable<T>集合按照指定的属性与排序方式进行再排序
/// <summary>
/// 把IOrderedQueryable集合按照指定的属性与排序方式进行排序
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="source">要排序的数据集</param>
/// <param name="propName">要排序的属性名称</param>
/// <param name="listSort">排序方式</param>
/// <returns>返回排序后的结果集</returns>
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propName,
ListSortDirection listSort = ListSortDirection.Ascending)
{
if (PublicHelper.CheckArgument(propName, "propName"))
{
return QueryableHelper<T>.ThenBy(source, propName, listSort);
}
return null;
}
#endregion #region 把IOrderedQueryable<T>集合按照指定的属性与排序方式进行再排序
/// <summary>
/// 把IOrderedQueryable集合按照指定的属性与排序方式进行排序
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="source">要排序的数据集</param>
/// <param name="sortCondition">排序条件</param>
/// <returns>返回排序后的结果集</returns>
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, PropertySortCondition sortCondition)
{
if (PublicHelper.CheckArgument(sortCondition, "sortCondition"))
{
return QueryableHelper<T>.ThenBy(source, sortCondition.PropertyName, sortCondition.ListSortDirection);
}
return null;
}
#endregion #region 多条件排序分页查询
/// <summary>
/// 把IQueryable集合按照指定的属性与排序方式进行排序后,再按照指定的条件提取指定页码指定条目数据
/// </summary>
/// <typeparam name="T">动态类型</typeparam>
/// <param name="source">要排序的数据集</param>
/// <param name="where">检索条件</param>
/// <param name="pageIndex">索引</param>
/// <param name="pageSize">页面大小</param>
/// <param name="total">总页数</param>
/// <param name="sortConditions">排序条件</param>
/// <returns>子集</returns>
public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, bool>> where, int pageIndex,
int pageSize, out int total, params PropertySortCondition[] sortConditions) where T : class, new()
{
IQueryable<T> temp = null; int i = 0;
if (PublicHelper.CheckArgument(source, "source")
&& PublicHelper.CheckArgument(where, "where")
&& PublicHelper.CheckArgument(pageIndex, "pageIndex")
&& PublicHelper.CheckArgument(pageSize, "pageSize")
&& PublicHelper.CheckArgument(sortConditions, "sortConditions"))
{
//判断是不是首个排序条件
int count = 0;
//得到满足条件的总记录数
i = source.Count(where);
//对数据源进行排序
IOrderedQueryable<T> orderSource = null;
foreach (PropertySortCondition sortCondition in sortConditions)
{
orderSource = count == 0
? source.OrderBy(sortCondition.PropertyName, sortCondition.ListSortDirection)
: orderSource.ThenBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
count++;
}
source = orderSource; temp = source != null
? source.Where(where).Skip((pageIndex - 1) * pageSize).Skip(pageSize)
: Enumerable.Empty<T>().AsQueryable();
} total = i;
return temp; //PublicHelper.CheckArgument(source, "source");
//PublicHelper.CheckArgument(where, "where");
//PublicHelper.CheckArgument(pageIndex, "pageIndex");
//PublicHelper.CheckArgument(pageSize, "pageSize");
//PublicHelper.CheckArgument(sortConditions, "sortConditions"); ////判断是不是首个排序条件
//int count = 0;
////得到满足条件的总记录数
//total = source.Count(where);
////对数据源进行排序
//IOrderedQueryable<T> orderSource = null;
//foreach (PropertySortCondition sortCondition in sortConditions)
//{
// orderSource = count == 0
// ? source.OrderBy(sortCondition.PropertyName, sortCondition.ListSortDirection)
// : orderSource.ThenBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
// count++;
//}
//source = orderSource; //return source != null
// ? source.Where(where).Skip((pageIndex - 1) * pageSize).Skip(pageSize)
// : Enumerable.Empty<T>().AsQueryable();
}
#endregion #region 多条件排序查询
///// <summary>
///// 多条件排序查询
///// </summary>
///// <typeparam name="T">动态类型</typeparam>
///// <param name="source">要排序的数据集</param>
///// <param name="where">检索条件</param>
///// <param name="sortConditions">排序条件</param>
///// <returns>子集</returns>
//public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, bool>> where, params PropertySortCondition[] sortConditions) where T : class, new()
//{
// log = LogManager.GetLogger(string.Format("查询表_{0}", typeof(T)));
// IQueryable<T> temp = null;
// Logger("", () =>
// {
// if (PublicHelper.CheckArgument(source, "source")
// && PublicHelper.CheckArgument(where, "where")
// && PublicHelper.CheckArgument(sortConditions, "sortConditions"))
// {
// //判断是不是首个排序条件
// int count = 0;
// //对数据源进行排序
// IOrderedQueryable<T> orderSource = null;
// foreach (PropertySortCondition sortCondition in sortConditions)
// {
// orderSource = count == 0
// ? source.OrderBy(sortCondition.PropertyName, sortCondition.ListSortDirection)
// : orderSource.ThenBy(sortCondition.PropertyName, sortCondition.ListSortDirection);
// count++;
// }
// source = orderSource; // temp = source != null
// ? source.Where(where)
// : Enumerable.Empty<T>().AsQueryable();
// }
// });
// return temp;
//}
#endregion #endregion #region IEnumerable<T>的扩展方法 #region 将集合展开分别转换成字符串,再以指定分隔字符串链接,拼成一个字符串返回
/// <summary>
/// 将集合展开分别转换成字符串,再以指定分隔字符串链接,拼成一个字符串返回
/// </summary>
/// <typeparam name="T">动态类型</typeparam>
/// <param name="collection">要处理的集合</param>
/// <param name="separator">分隔符</param>
/// <returns>拼接后的字符串</returns>
public static string ExpandAndToString<T>(this IEnumerable<T> collection, string separator)
{
List<T> source = collection as List<T> ?? collection.ToList();
if (source.IsEmpty())
{
return "";
} string result = source.Cast<object>()
.Aggregate<object, string>(null, (current, o) => current + String.Format("{0}{1}", o, separator)); return result.Substring(0, result.LastIndexOf(separator, StringComparison.Ordinal));
}
#endregion #region 根据指定条件返回集合中不重复的元素
/// <summary>
/// 根据指定条件返回集合中不重复的元素
/// </summary>
/// <typeparam name="T">动态类型</typeparam>
/// <typeparam name="TKey">动态筛选条件类型</typeparam>
/// <param name="source">要操作的数据源</param>
/// <param name="keySelector">重复数据的筛选条件</param>
/// <returns>不重复元素的集合</returns>
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
{
//取分组集合中每组的第一条数据
return source.GroupBy(keySelector).Select(group => group.First());
}
#endregion #region 根据第三方条件是否为真是否执行指定条件的查询
/// <summary>
/// 根据第三方条件是否为真是否执行指定条件的查询
/// </summary>
/// <typeparam name="T">动态类型</typeparam>
/// <param name="source">要查询的数据源</param>
/// <param name="where">条件</param>
/// <param name="condition">第三方条件</param>
/// <returns>查询的结果</returns>
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, Func<T, bool> where,
bool condition)
{
return condition ? source.Where(where) : source;
}
#endregion #region 判断集合是否为空
/// <summary>
/// 判断集合是否为空
/// </summary>
/// <typeparam name="T">动态类型</typeparam>
/// <param name="collection">要处理的集合</param>
/// <returns>集合为空返回true 不为空返回false</returns>
public static bool IsEmpty<T>(this IEnumerable<T> collection)
{
return !collection.Any();
}
#endregion #endregion #region 其他
/// <summary>
/// 扩展Between 操作符
/// 使用 var query = db.People.Between(person => person.Age, 18, 21);
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source">当前值</param>
/// <param name="keySelector">拉姆达表达式</param>
/// <param name="low">低</param>
/// <param name="high">高</param>
/// <returns></returns>
public static IQueryable<TSource> Between<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, TKey low, TKey high) where TKey : IComparable<TKey>
{
Expression key = Expression.Invoke(keySelector, keySelector.Parameters.ToArray()); Expression lowerBound = Expression.GreaterThanOrEqual(key, Expression.Constant(low)); Expression upperBound = Expression.LessThanOrEqual(key, Expression.Constant(high)); Expression and = Expression.AndAlso(lowerBound, upperBound); Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(and, keySelector.Parameters); return source.Where(lambda);
} /// <summary>
/// In
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="values"></param>
/// <returns></returns>
public static bool In<T>(this T value, params T[] values)
{
return values.Contains(value);
} /// <summary>
/// Between
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
public static bool Between<T>(this T i, T start, T end) where T : IComparable<T>
{
return i.CompareTo(start) >= 0 && i.CompareTo(end) <= 0;
} /// <summary>
/// And
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool And(this bool left, bool right)
{
return left && right;
} /// <summary>
/// Or
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool Or(this bool left, bool right)
{
return left || right;
} #endregion

  

C#对IQueryable<T>、IEnumerable<T>的扩展方法的更多相关文章

  1. IEnumerable接口的扩展方法

    /// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...

  2. IEnumerable<T>与IQueryable<T>以及.net的扩展方法

    首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...

  3. 工作总结 for 另类写法 循环加时间 集合合并 也是用的 static class Enumerable (IEnumerable<T>的扩展方法) (IEnumerable<T> 的 工具类) (所有集合 数组都实现IEnumerable<T>)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 23.C#Queryable的扩展方法(十二章12.1-12.2)

    今天要写的知识还真心有点绕呢,对于第一节的内容,其实是把原先在内存中的数据源,换成了从数据库中提取出来的数据.从代码的使用方式上是一样的,直接跳过,来看看IEnumerable和IQueryable的 ...

  5. 从扩展方法到匿名方法再到LINQ

    1.首先我们应该知道什么是扩展方法: 扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...

  6. C#语法糖之第四篇: 扩展方法

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  7. C#语法糖: 扩展方法(常用)

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  8. C# LiNq的语法以及常用的扩展方法

    首先先来扯一下,这篇博文是我第一次写的,主要是我的一些摘录,希望对大家有所帮助. Linq的基础 •LINQ(读音link):Linq To SQL(过时).Linq To Object.Linq T ...

  9. 3.4.1 使用过滤式扩展方法(P43-44)

    对IEnumerable<T>执行标准并且同样返回IEnumerable<T>的扩展方法,可以使用yield关键字对源数据中的项应用选择标准,已生成精简的结果集. public ...

随机推荐

  1. RabbitMQ与Spring集成配置

    1.引入相关jar包 //RabbitMQ compile group: 'org.springframework.amqp', name: 'spring-rabbit', version: '1. ...

  2. [Cypress] Use the Most Robust Selector for Cypress Tests

    Which selectors your choose for your tests matter, a lot. In this lesson, we'll see the recommended ...

  3. No module named 'pip._vendor.progress.helpers' 的解决方法

    莫名其妙的pip出现故障了 ModuleNotFoundError: No module named ‘pip._vendor.progress.helpers’ 在百度搜索了一圈也没看到这个错误的解 ...

  4. P1129 [ZJOI2007]矩阵游戏 二分图匹配

    思路:脑子+二分图匹配 提交:1次(课上讲过) 题解: 发现:如果符合题意,那么行和列一定是一一匹配的(必要条件),所以最大匹配必须是$n$. 同时我们发现,一定可以通过交换行列的方式,将(看起来)有 ...

  5. (三)根据向导创建MFC工程,事件的添加和删除

    一,文档视图结构 文档:它是一个类,这个类专门用来存储数据 视图:它是一个类,这个类专门用来显示和修改数据 框架类:一个容器,这个容器装了视图 健完工程之后,类视图: 运行一下: 几个比较重要的函数 ...

  6. hdu 1023 hdu 1131

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  7. SpringMVC——拦截器,过滤器实现登录拦截

    一.拦截器与过滤器的区别 1.过滤器 依赖于servlet容器.在实现上基于函数回调,可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次.使用过滤器的目的是用来做一些过滤操 ...

  8. Postman官方文档翻译

    本文根据官方文档,为千夜同学的翻译做了一个目录 启动Postman Postman的安装和升级 发送第一个请求 创建第一个集合 Postman导航 Postman账号 同步 设置 新按钮 发送API请 ...

  9. 【redis 学习系列07】Redis小功能大用处01 慢查询分析以及Redis Shell

    Redis提供了5种数据结构已经足够强大,但除此之外,Redis还提供了诸如慢查询分析.功能强大的Redis Shell.Pipeline.事务与Lua脚本.Bitmaps.HyperLogLog.发 ...

  10. Resharper错误提示方法的命名

    Resharper-->Options-->C#-->Naming Style