You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.2 KiB
C#
100 lines
3.2 KiB
C#
// ***********************************************************************
|
|
// Assembly : Construction
|
|
// Author : zjx
|
|
// Created : 09-16-2020
|
|
//
|
|
// Last Modified By : zjx
|
|
// Last Modified On : 09-16-2020
|
|
// ***********************************************************************
|
|
// <copyright file="EnumerableExtensions.cs" company="jindongfang">
|
|
// Copyright (c) jindongfang. All rights reserved.
|
|
// </copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace WellWorkDataUI
|
|
{
|
|
/// <summary>
|
|
/// Class EnumerableExtensions.
|
|
/// </summary>
|
|
public static class EnumerableExtensions
|
|
{
|
|
/// <summary>
|
|
/// Orders the by list.
|
|
/// </summary>
|
|
/// <typeparam name="TSource">The type of the t source.</typeparam>
|
|
/// <typeparam name="TOrder">The type of the t order.</typeparam>
|
|
/// <param name="source">The source.</param>
|
|
/// <param name="order">The order.</param>
|
|
/// <param name="equal">The equal.</param>
|
|
/// <returns>IEnumerable<TSource>.</returns>
|
|
public static IEnumerable<TSource> OrderByList<TSource, TOrder>(this IEnumerable<TSource> source, IEnumerable<TOrder> order, Func<TSource, TOrder, bool> equal)
|
|
{
|
|
IEnumerable<TSource> result = from s in source
|
|
from o in order.Select((v, i) => new { obj = v, index = i })
|
|
where equal(s, o.obj)
|
|
orderby o.index
|
|
select s;
|
|
|
|
return result.Union(source.Where(r => !order.Any(k => equal(r, k))));
|
|
}
|
|
|
|
/// <summary>
|
|
/// MinOrDefault
|
|
/// </summary>
|
|
/// <typeparam name="T">t</typeparam>
|
|
/// <param name="sequence">sequence</param>
|
|
/// <returns>T</returns>
|
|
public static T MinOrDefault<T>(this IEnumerable<T> sequence)
|
|
{
|
|
if (sequence.Any())
|
|
{
|
|
return sequence.Min();
|
|
}
|
|
else
|
|
{
|
|
return default(T);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// MaxOrDefault
|
|
/// </summary>
|
|
/// <typeparam name="T">t</typeparam>
|
|
/// <param name="sequence">sequence</param>
|
|
/// <returns>T</returns>
|
|
public static T MaxOrDefault<T>(this IEnumerable<T> sequence)
|
|
{
|
|
if (sequence.Any())
|
|
{
|
|
return sequence.Max();
|
|
}
|
|
else
|
|
{
|
|
return default(T);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SumOrDefault
|
|
/// </summary>
|
|
/// <typeparam name="T">t</typeparam>
|
|
/// <param name="sequence">sequence</param>
|
|
/// <returns>T</returns>
|
|
public static double? SumOrDefault(this IEnumerable<double?> sequence)
|
|
{
|
|
if (sequence.Any())
|
|
{
|
|
return sequence.Sum();
|
|
}
|
|
else
|
|
{
|
|
return default(double?);
|
|
}
|
|
}
|
|
}
|
|
}
|