C# 如何使用lambda表达式更改计算类型?

C# 如何使用lambda表达式更改计算类型?,c#,linq,lambda,C#,Linq,Lambda,我有一个基于某个ID或字符串值的列表,我想通过group by更改计算类型 var result = from r in mlist group r by new { r.ParameterId, r.WId, } into g select new

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

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)
                     };
我想用一个自定义方法替换这个linqSum,该方法将返回基于某些计算类型(如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)
                     };
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;
    }
}

通过向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);
        }
    }
}
公共静态类LINQExtension
{
公共静态双中位数(此IEnumerable源)
{
if(source.Count()==0)
{
抛出新的InvalidOperationException(“无法计算空集的中值”);
}
var sortedList=来自源中的编号
订单号
选择号码;
int itemIndex=(int)sortedList.Count()/2;
if(sortedList.Count()%2==0)
{
//偶数个项目。
返回(sortedList.ElementAt(itemIndex)+sortedList.ElementAt(itemIndex-1))/2;
}
其他的
{
//奇数项。
返回分类列表元素(itemIndex);
}
}
}


您还想查看

您可以通过向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);
        }
    }
}
公共静态类LINQExtension
{
公共静态双中位数(此IEnumerable源)
{
if(source.Count()==0)
{
抛出新的InvalidOperationException(“无法计算空集的中值”);
}
var sortedList=来自源中的编号
订单号
选择号码;
int itemIndex=(int)sortedList.Count()/2;
if(sortedList.Count()%2==0)
{
//偶数个项目。
返回(sortedList.ElementAt(itemIndex)+sortedList.ElementAt(itemIndex-1))/2;
}
其他的
{
//奇数项。
返回分类列表元素(itemIndex);
}
}
}


您还想查看

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

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)
                     };
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;
    }
}
如果要为所有项目创建一个calctype,可以编写以下内容:

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

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

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)
                     };
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;
    }
}
如果要为所有项目创建一个calctype,可以编写以下内容:

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

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