zoukankan      html  css  js  c++  java
  • ABP框架源码中的Linq扩展方法

    文件目录:aspnetboilerplate-devaspnetboilerplate-devsrcAbpCollectionsExtensionsEnumerableExtensions.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) }; }

    以前都需要这样写:

  • 相关阅读:
    查看centos版本
    Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException 拒绝访问 / 出现了内部错误 c# – 当使用X509Certificate2加载p12/pfx文件时出现
    asp.net asp.net application 升级到 asp.net web 解决找不到控件 批量生成.designer文件
    netcore发布到 iis 设置 部署 环境 变量
    sqlserver 3145
    windows server 2012 远程桌面不好使
    VirtualBox 桥接模式,虚拟机ping不通宿主机
    移动端笔记
    css笔记——关于css中写上charset “utf-8”
    工作笔记——前端分页数据回显
  • 原文地址:https://www.cnblogs.com/wanghaibin/p/6562066.html
Copyright © 2011-2022 走看看