ABP框架源码中的Linq扩展方法
文件目录:aspnetboilerplate-dev\aspnetboilerplate-dev\src\Abp\Collections\Extensions\EnumerableExtensions.cs
using System;
using System.Collections.Generic;
using System.Linq; namespace Abp.Collections.Extensions
{
/// <summary>
/// Extension methods for <see cref="IEnumerable{T}"/>.
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// Concatenates the members of a constructed <see cref="IEnumerable{T}"/> collection of type System.String, using the specified separator between each member.
/// This is a shortcut for string.Join(...)
/// </summary>
/// <param name="source">A collection that contains the strings to concatenate.</param>
/// <param name="separator">The string to use as a separator. separator is included in the returned string only if values has more than one element.</param>
/// <returns>A string that consists of the members of values delimited by the separator string. If values has no members, the method returns System.String.Empty.</returns>
public static string JoinAsString(this IEnumerable<string> source, string separator)
{
return string.Join(separator, source);
} /// <summary>
/// Concatenates the members of a collection, using the specified separator between each member.
/// This is a shortcut for string.Join(...)
/// </summary>
/// <param name="source">A collection that contains the objects to concatenate.</param>
/// <param name="separator">The string to use as a separator. separator is included in the returned string only if values has more than one element.</param>
/// <typeparam name="T">The type of the members of values.</typeparam>
/// <returns>A string that consists of the members of values delimited by the separator string. If values has no members, the method returns System.String.Empty.</returns>
public static string JoinAsString<T>(this IEnumerable<T> source, string separator)
{
return string.Join(separator, source);
} /// <summary>
/// Filters a <see cref="IEnumerable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="source">Enumerable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="predicate">Predicate to filter the enumerable</param>
/// <returns>Filtered or not filtered enumerable based on <paramref name="condition"/></returns>
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, bool> predicate)
{
return condition
? source.Where(predicate)
: source;
} /// <summary>
/// Filters a <see cref="IEnumerable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="source">Enumerable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="predicate">Predicate to filter the enumerable</param>
/// <returns>Filtered or not filtered enumerable based on <paramref name="condition"/></returns>
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, int, bool> predicate)
{
return condition
? source.Where(predicate)
: source;
}
}
}
WhereIf很好用:
public GetTasksOutput GetTasks(GetTasksInput input)
{
var query = _taskRepository.GetAll().Include(t => t.AssignedPerson)
.WhereIf(input.State.HasValue, t => t.State == input.State.Value)
.WhereIf(!input.Filter.IsNullOrEmpty(), t => t.Title.Contains(input.Filter))
.WhereIf(input.AssignedPersonId.HasValue, t => t.AssignedPersonId == input.AssignedPersonId.Value);
//WhereIf为Linq扩展方法,表示如果第一个表达式为true,则加第二个过滤条件
//这样就不需要用那么多个if()了 //排序
if (!string.IsNullOrEmpty(input.Sorting))
query = query.OrderBy(input.Sorting);
else
query = query.OrderByDescending(t => t.CreationTime); var taskList = query.ToList(); //Used AutoMapper to automatically convert List<Task> to List<TaskDto>.
return new GetTasksOutput
{
Tasks = Mapper.Map<List<TaskDto>>(taskList)
};
}
以前都需要这样写:

ABP框架源码中的Linq扩展方法的更多相关文章
- ABP框架源码学习之修改默认数据库表前缀或表名称
ABP框架源码学习之修改默认数据库表前缀或表名称 1,源码 namespace Abp.Zero.EntityFramework { /// <summary> /// Extension ...
- 手把手带你撸一把springsecurity框架源码中的认证流程
提springsecurity之前,不得不说一下另外一个轻量级的安全框架Shiro,在springboot未出世之前,Shiro可谓是颇有统一J2EE的安全领域的趋势. 有关shiro的技术点 1.s ...
- ABP框架源码学习之授权逻辑
asp.net core的默认的几种授权方法参考"雨夜朦胧"的系列博客,这里要强调的是asp.net core mvc中的授权和asp.net mvc中的授权不一样,建议先看前面& ...
- CI框架源码阅读笔记6 扩展钩子 Hook.php
CI框架允许你在不修改系统核心代码的基础上添加或者更改系统的核心功能(如重写缓存.输出等).例如,在系统开启hook的条件下(config.php中$config['enable_hooks'] = ...
- React 源码中的依赖注入方法
一.前言 依赖注入(Dependency Injection)这个概念的兴起已经有很长时间了,把这个概念融入到框架中达到出神入化境地的,非Spring莫属.然而在前端领域,似乎很少会提到这个概念,难道 ...
- jquery源码解析:jQuery扩展方法extend的详解
jQuery中要扩展方法或者属性都是通过extend方法实现的.所谓的jQuery插件也是通过extend方法实现的. jQuery.extend扩展的是工具方法,也就是静态方法.jQuery.fn. ...
- netty学习--netty源码中的部分util方法
io.netty.buffer.AbstractByteBuf#calculateNewCapacity 申请内存空间 private int calculateNewCapacity(int mi ...
- [Abp vNext 源码分析] - 2. 模块系统的变化
一.简要说明 本篇文章主要分析 Abp vNext 当中的模块系统,从类型构造层面上来看,Abp vNext 当中不再只是单纯的通过 AbpModuleManager 来管理其他的模块,它现在则是 I ...
- iOS学习——布局利器Masonry框架源码深度剖析
iOS开发过程中很大一部分内容就是界面布局和跳转,iOS的布局方式也经历了 显式坐标定位方式 --> autoresizingMask --> iOS 6.0推出的自动布局(Auto La ...
随机推荐
- Foundation-NSRunLoop
Objective-C之run loop详解 Objective-C之run loop详解 RunLoop 详解
- 5、Makefile基础知识汇总(转自陈皓总述)
一.Makefile里有什么? Makefile里主要包含了五个东西:显式规则.隐晦规则.变量定义.文件指示和注释. 1.显式规则.显式规则说明了,如何生成一个或多的的目标文件.这是由Makefile ...
- JAVA本地读取文件,解决中文乱码问题
JAVA本地读取文件出现中文乱码,查阅一个大神的博客做一下记录 import java.io.BufferedInputStream;import java.io.BufferedReader;imp ...
- ASP.NET Web API 入门 (API接口、寄宿方式、HttpClient调用)
一.ASP.NET Web API接口定义 ASP.NET Web API默认实现了Action方法和HTTP方法的映射,Action方法方法名体现了其能处理的请求必须采用的HTTP方法 二.寄宿方式 ...
- C - Roll-call in Woop Woop High
Description The new principal of Woop Woop High is not satisfied with her pupils performance. She in ...
- 在Delphi中处理word文档与数据库的互联 1
在Delphi中处理word文档与数据库的互联 ---- 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具.在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可 ...
- Network in Network 2
<Network in Network>论文笔记 1.综述 这篇文章有两个很重要的观点: 1×1卷积的使用 文中提出使用mlpconv网络层替代传统的convolution层.mlp层实际 ...
- Lua C API 遍历 table
http://timothyqiu.com/archives/lua-note-table-traversal-using-c-api/ C API 遍历 Table lua_getglobal(L, ...
- 我的AI之路 —— OCR文字识别快速体验版
OCR的全称是Optical Character Recoginition,光学字符识别技术.目前应用于各个领域方向,甚至这些应用就在我们的身边,比如身份证的识别.交通路牌的识别.车牌的自动识别等等. ...
- C#6.0语言规范(十) 类
类是可以包含数据成员(常量和字段),函数成员(方法,属性,事件,索引器,运算符,实例构造函数,析构函数和静态构造函数)和嵌套类型的数据结构.类类型支持继承,这是一种派生类可以扩展和专门化基类的机制. ...