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 ... 
随机推荐
- (动态规划)matrix -- hdu -- 5569
			http://acm.hdu.edu.cn/showproblem.php?pid=5569 matrix Time Limit: 6000/3000 MS (Java/Others) Memo ... 
- PAT甲级 1127. ZigZagging on a Tree (30)
			1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ... 
- Lib作为“静态库”与“动态库”中的区别
			Lib作为“静态库”与“动态库”中的区别 0. 前言: 什么是静态连接库: 静态库在链接阶段,会将汇编生成的目标文件.o与引用到的库一起链接打包到可执行文件中.因此对应的链接方式称为静态链接. 为什么 ... 
- SRM483
			250pt 题意:给定一个[0,1)间的实数,一个分母不超过maxDen的分数逼近.. 思路:直接枚举.然后判断. code: #line 7 "BestApproximationDiv1. ... 
- Android-Retrofit-2.0-Post与Get-请求有道词典翻译
			Retrofit-2.0版本后,内置已经集成了OKHttp,在使用Retrofit的时候 看似是Retrofit去网络请求的 实际上Retrofit只是封装,所以不要以为Retrofit是网络请求框架 ... 
- 什么是PAGELATCH和PAGEIOLATCH
			在分析SQL server 性能的时候你可能经常看到 PAGELATCH和PAGEIOLATCH.比方说 Select * from sys.dm_os_wait_stats 的输出里面就有Latch ... 
- c# file 上传EXCEL文件,以流的形式读取数据
			1.引入 Aspose.Cells public void test() { HttpFileCollection filelist = HttpContext.Current.Request.Fi ... 
- Deque(队列)
			目录 Deque 概述 特点 常用方法 双向队列操作 ArrayDeque Deque 概述 一个线性 collection,支持在两端插入和移除元素.名称 deque 是"double e ... 
- [学习笔记]Link-Cut Tree
			我终于理解了 \(LCT\)!!!想不到小蒟蒻有一天理解了!!! 1.[模板]Link Cut Tree 存个板子 #include <bits/stdc++.h> using names ... 
- sublime text 文件打开时回调一些函数
			需求:公司服务端脚本以 .s 结尾的文件,也按 js 语法识别,方便查看函数定义. 每次都 ss:js 比较麻烦,所以写个插件. import sublime, sublime_plugin clas ... 
