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 ...
随机推荐
- java基础-day13
第01天 java面向对象 今日内容介绍 u 继承 u 抽象类 第1章 继承 1.1 继承的概述 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继 ...
- 12.Scrapy与mongodb交互和设置中间键
反反爬虫相关机制 Some websites implement certain measures to prevent bots from crawling them, with varying d ...
- [leetcode 120]triangle 空间O(n)算法
1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- Excel 多个单元格输入同样内容
step1: 将这些单元格选定.方法:可以连续选,也可以 ctrl + select不连续选择: step2:输入你想输入的内容,PS:出现在最后选择的单元格中: step3:组合键:ctrl + e ...
- caffe 训练imagenet
1.整理得到自己的数据库,并生成自己数据库的列表文件.txt 2.将数据库转成lmbp格式 3.计算图像均值 4.修改网络参数 5.得到结果 1.整理得到自己的数据库 因为前面博文提到的原因,技术水平 ...
- Azure DevOps Server(TFS 2019) 中的SonarQube扫描任务出现错误:AppTest.java can't be indexed twice
SonarQube错误描述 将一个Maven示例程序导入到Azure DevOps的待库中,执行SonarQube扫描过程时, DevOps Server提示下面的错误信息: [ERROR] Fail ...
- sql server rdl report 如何用动态sql
我做rdl report 一般用存储过程,可是今天遇到个问题,需要用动态sql,rdl report数据集不能绑定字段 查了一下谷歌,解决如下: declare @CarrierList table ...
- 使用System.Net.Mail中的SMTP发送邮件(带附件)
System.Net.Mail 使用简单邮件传输协议SMTP异步发送邮件 想要实现SMTP发送邮件,你需要了解这些类 SmtpClient :使用配置文件设置来初始化 SmtpClient类的新实例. ...
- sql server 查看列备注、类型、字段大小
select 列名 = a.name ,类型 = c.name ,长度 = columnproperty(a.id,a.name,'precision') ,备注 = g.value from sys ...
- Poetry
1. Absence to love is what wind is to fire. It extinguishes the small; It inflames the great. 2. It ...