C#linq是否具有与SummaryStatistics类似的东西?

时间:2018-08-26 13:20:23

标签: c# .net linq

在Java中,我可以这样做:

List<String> stringList = getRandomStrings(100_000, 1000);
IntSummaryStatistics stats =
                stringList.stream()
                        .filter(Objects::nonNull)
                        .mapToInt(String::length)
                        .summaryStatistics();

System.out.println(stats);

输出为:

IntSummaryStatistics{count=100000, sum=49868013, min=0, average=498.680130, max=999}

linq是否具有相同的功能?还是我必须分别计算所有内容?

Java一口气做到了这一点,看起来像在C#中,您必须计算所有成分Average()Count()Sum()Min()和{{1 }}。

1 个答案:

答案 0 :(得分:13)

我不知道.NET内置的功能是否等效,但是您可以使用LINQ的Aggregate自己轻松实现。这满足了您只枚举源集合一次的要求。

var ints = new List<int>() { 1, 2, 3, 4, 677, 8 };

var summary = ints.Aggregate(
    seed: (
        count: 0, 
        sum: 0, 
        min: int.MaxValue, 
        max: int.MinValue),
    func: (acc, x) => (
        count: acc.count + 1, 
        sum: acc.sum + x, 
        min: Math.Min(acc.min, x), 
        max: Math.Max(acc.max, x)),
    resultSelector: acc => (
        acc.count, 
        acc.sum, 
        acc.min, 
        acc.max, 
        avg: (double)acc.sum / acc.count));

Console.WriteLine(
    $"count = {summary.count}, " +
    $"sum = {summary.sum}, " +
    $"min = {summary.min}, " +
    $"max = {summary.max}, " +
    $"average = {summary.avg}");

如果您的集合很大,并且您想并行化其处理,则可以使用此重载:

var summary = ints.AsParallel().Aggregate(
    seed: (
        count: 0,
        sum: 0,
        min: int.MaxValue,
        max: int.MinValue),
    updateAccumulatorFunc: (acc, x) => (
        count: acc.count + 1,
        sum: acc.sum + x,
        min: Math.Min(acc.min, x),
        max: Math.Max(acc.max, x)),
    combineAccumulatorsFunc: (acc1, acc2) => (
        count: acc1.count + acc2.count,
        sum: acc1.sum + acc2.sum,
        min: Math.Min(acc1.min, acc2.min),
        max: Math.Max(acc1.max, acc2.max)),
    resultSelector: acc => (
        acc.count,
        acc.sum,
        acc.min,
        acc.max,
        avg: (double)acc.sum / acc.count));