【问题标题】:How to change calculation type using lambda expression?如何使用 lambda 表达式更改计算类型?
【发布时间】:2015-12-15 15:56:35
【问题描述】:

我有一个基于某个 ID 或字符串值的列表,我想通过分组更改计算类型

var result = from r in mlist
             group r by new
                     {
                         r.ParameterId,
                         r.WId,

                     } into g
             select new
                     {
                         g.Key.ParameterId,
                         g.Key.WId,
                         Value = g.Sum(x => x.Value)
                     };

我想将这个 linq Sum 替换为自定义方法,该方法将返回基于某种计算类型(如 avg、sum 等)的计算结果。

可能是这样的:

var result = from r in mlist
             group r by new
                     {
                         r.ParameterId,
                         r.WId,
                     } into g
             select new
                     {
                         g.Key.ParameterId,
                         g.Key.WId,
                         Value = g.CustomMethod(x => x.Value, x.calctype)
                     };

【问题讨论】:

  • 这会撞到数据库吗?还是在记忆中?无论如何,这就像在您的自定义方法中编写 if/switch 语句一样简单。
  • g.CustomMethod(x => x.Value, x.calctype) 中的第二个x 来自哪里?也许可以在字典中添加所有可能的函数,以calctype 作为键。在这种情况下,您可以将其称为dict[<calctype>](g, x => x.Value)。使用它你不能像扩展方法一样调用它。
  • 是否需要专门将x.value和x.calctype传递给CustomMethod?如果您可以访问 CustomMethod 中的完整 r 对象,它不会起作用吗?
  • Rob:它在内存中 - 我不能像我提到的那样调用 custommethod 请建议更好的方法来调用方法。

标签: c# linq lambda


【解决方案1】:

您可以通过向 IEnumerable 接口添加扩展方法来扩展可用于 LINQ 查询的方法集。例如,除了标准的平均值或最大值运算之外,您还可以创建自定义聚合方法来从值序列中计算单个值。您还可以创建一个方法,该方法用作自定义过滤器或用于一系列值的特定数据转换并返回一个新序列。

public static class LINQExtension
{
    public static double Median(this IEnumerable<double> source)
    {
        if (source.Count() == 0)
        {
            throw new InvalidOperationException("Cannot compute median for an empty set.");
        }

        var sortedList = from number in source
                         orderby number
                         select number;

        int itemIndex = (int)sortedList.Count() / 2;

        if (sortedList.Count() % 2 == 0)
        {
            // Even number of items.
            return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2;
        }
        else
        {
            // Odd number of items.
            return sortedList.ElementAt(itemIndex);
        }
    }
}

Source: Add Custom Methods for LINQ Queries

您还想查看Create the function using Expression linq

【讨论】:

    【解决方案2】:

    我认为您应该编写自己的扩展方法,如下所示:

    public static class MyEnumerable
    {
        public static int CustomMethod<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector, Func<TSource, int> type)
        {
            var sum = 0;
            source.ToList().ForEach(x => sum += selector(x) * type(x));
            return sum;
        }
    }
    

    你会这样执行,你的第二个代码清单不会被编译:

    Value = g.CustomMethod(x => x.Value, x => x.calctype)
    

    如果你想为所有项目使用一个 calctype,你可以这样写:

    Value = g.CustomMethod(x => x.Value, x => 123);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多