ABP框架源码中的Linq扩展方法
2017-03-16 22:41
309 查看
文件目录:aspnetboilerplate-dev\aspnetboilerplate-dev\src\Abp\Collections\Extensions\EnumerableExtensions.cs
WhereIf很好用:
以前都需要这样写:
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) }; }
以前都需要这样写:
相关文章推荐
- CI框架源码解读之利用Hook.php文件完成功能扩展的方法
- ABP源码分析十五:ABP中的实用扩展方法
- CI框架源码解读之利用Hook.php文件完成功能扩展的方法
- LINQ学习(扩展方法,委托,Lambda表达式) 第一篇
- 关于 Linq 中的 Aggregate 扩展方法的实例
- linq to sql 扩展方法
- 为LINQ服务的C#新特性总结篇---扩展方法,匿名委托,lambda表达式,Action委托,Func委托,Linq中的order by,top和sum函数
- 一个用于格式化/统计文本的可扩展框架(附源码)
- CodeIgniter源码阅读(6)Hook.php 扩展框架的实现原理
- 使用Enumerable.OfType<T>扩展方法实现非泛型集合的Linq查询
- Linq原理(扩展方法)
- 本周ASP.NET英文技术文章推荐[09/23 - 09/29]:IIS 7.0、Facebook.NET、ASP.NET AJAX、ModalPopupExtender、扩展方法、LinqDataSource、ListView、Visual Studio
- [C#3.0体验]Orcas中内置的LinQ,XLinQ[DLinQ]扩展方法
- 开源搜索框架Lucene学习之分词器(3)——通过分词器源码学习抽象方法与虚方法的区别
- 2012最新ANDROID框架源码下载方法
- Linq学习总结(2)——扩展方法
- 综合应用WPF/WCF/WF/LINQ之二十:ListView控件中几个常用的扩展方法
- 【收藏】本周ASP.NET英文技术文章推荐[09/23 - 09/29]:IIS 7.0、Facebook.NET、ASP.NET AJAX、ModalPopupExtender、扩展方法、LinqDataSource、ListView、Visual Studio
- LINQ学习笔记之二:扩展方法与隐藏类型
- 《ASP.NET MVC验证框架中关于属性标记的通用扩展方法》之继续扩展