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)
            };
        }

以前都需要这样写:

时间: 2024-08-04 12:57:44

ABP框架源码中的Linq扩展方法的相关文章

ABP框架源码学习之修改默认数据库表前缀或表名称

1,源码 1 namespace Abp.Zero.EntityFramework 2 { 3 /// <summary> 4 /// Extension methods for <see cref="DbModelBuilder"/>. 5 /// </summary> 6 public static class AbpZeroDbModelBuilderExtensions 7 { 8 /// <summary> 9 /// Chan

CI框架源码阅读笔记6 扩展钩子 Hook.php

CI框架允许你在不修改系统核心代码的基础上添加或者更改系统的核心功能(如重写缓存.输出等).例如,在系统开启hook的条件下(config.php中$config['enable_hooks'] = TRUE;),通过添加特定的钩子,可以让系统在特定的时刻触发特定的脚本: $hook['post_system'] = array( 'class' => 'frameLog', 'function' => 'postLog', 'filename' => 'post_system.php'

jQuery源码中的Ajax--getScript()/getJson()方法

一.$.getScript()方法 有时候,在页面初次加载时就取得所需的全部Javascript文件是完全没必要的,可以按需所取. 该函数用于动态加载JS文件,并在全局作用域下执行文件中的JS代码. 该函数可以加载跨域的JS文件.请注意,该函数是通过异步方式加载数据的. 该函数属于全局jQuery对象. 语法: $(function(){ $("send").on("click",function(){ $.getScript("script.js&quo

jQuery源码中的Ajax--get()/post()方法

load()方法通常用来在web服务器上获取静态的数据文件,如果需要传递一些参数给服务器中的页面,那就可以使用$.get()方法或$.post()方法. *$.get()方法和$.post()方法是jQuery中的全局函数 一.$.get()方法 $.get()方法是使用GET方式来进行异步请求.结构为: $.get(url [.data] [.callback] [.type]) 参数解释如下: 参数名称 类型 说明 url String 请求的HTML页的url地址 data(可选) Obj

jquery源码解析:jQuery扩展方法extend的详解

jQuery中要扩展方法或者属性都是通过extend方法实现的.所谓的jQuery插件也是通过extend方法实现的. jQuery.extend扩展的是工具方法,也就是静态方法.jQuery.fn.extend扩展的是实例方法. 当只传入一个对象的时候,里面的方法和属性是扩展到this上的.比如: $.extend( { aaa:function(){}, bbb:function(){} } ) ,这里的this是$,所以用这种形式$.aaa()调用. $.fn.extend( { aaa:

EventBus框架源码分析

开源项目 上周又手动撸了一遍EventBus实现,同时上传EventBus的中文注释源码到Github上,欢迎大家fork&star. EventBusAnalysis EventBus 基础概念 EventBus是一个Android事件发布/订阅框架,通过解耦发布者和订阅者简化Android事件传递.事件传递既可以用于Android四大组件间的通讯,也可以用于用户异步线程和主线程间通讯等. 传统的事件传递方法包括:Handler,BroadCastReceiver,interface回调,相比

java源码中的注解

spring框架源码中充满了注解,如果对注解不是很了解,阅读源码就寸步难行,下面我们来看看annotation.https://blog.csdn.net/briblue/article/details/73824058,这篇文章虽然有点长,但是基本都讲到了.当开发者使用了Annotation 修饰了类.方法.Field 等成员之后,这些 Annotation 不会自己生效,必须由开发者提供相应的代码来提取并处理 Annotation 信息,这就是annotation的原理,给开发者提供了很大创

Android 网络框架之Retrofit2使用详解及从源码中解析原理

就目前来说Retrofit2使用的已相当的广泛,那么我们先来了解下两个问题: 1 . 什么是Retrofit? Retrofit是针对于Android/Java的.基于okHttp的.一种轻量级且安全的.并使用注解方式的网络请求框架. 2 . 我们为什么要使用Retrofit,它有哪些优势? 首先,Retrofit使用注解方式,大大简化了我们的URL拼写形式,而且注解含义一目了然,简单易懂: 其次,Retrofit使用简单,结构层次分明,每一步都能清晰的表达出之所以要使用的寓意: 再者,Retr

Android Eclipse中查看 Android框架源码

有时候用Eclipse想按住ctrl键查看源码怎么办? 下面具体步骤让你轻松看源码: project->properties->java build path->libraries 点击android.jar下面的source: 这里可以添加zip和文件夹,zip可以去git下载,我这里用的是用sdk manager下载的源码,如下: 从这里面下载的源码就保存在sdk下面的source下面,选一个平台关联就可以了 下面就是button源码: @RemoteView public clas