问题描述
我是使用lambda表达式的初学者.
I am beginner at using lambda expressions.
我有一个经销商列表,每个经销商我都必须计算等级.
I have a list of dealers, foreach dealer I have to calculate grade.
要求将成绩计算分为单独的方法.
The request is that the grade calculation to be separated into a separate method.
因此,我正在编写以下两种方法,但是无法将参数传递给 CalculateGrade()
方法,
So I am writing the below two methods, however I am unable to pass parameters to CalculateGrade()
method,
public IEnumerable<Dealers> GetDealerGrades(IEnumerable<Dealers> gradeData)
{
return gradeData
.GroupBy(row => new { row.Name })
.Select(g => new Dealers
{
Name = g.Key.Name,
TotalPoints = CalculateGrade(x => Convert.ToDouble(x.RecievedPoints),
y => y.MaxPoints,
z => Convert.ToDouble(z.Weightage))
})
.ToList();
}
private double CalculateGrade(double d1, int d2, double d3)
{
return ( d1 / d2 )
* d3 == 0.00 ? 1
: d3;
}
有人可以建议如何在其中传递参数,或者如何传递lamda表达式并计算成绩吗?
Can somebody advise how to pass parameters in this , or how to pass lamda expressions and calculate grade?
非常感谢...
推荐答案
看来您需要:
return gradeData
.GroupBy(row => row.Name)
.Select(g => new Dealers
{
Name = g.Key.Name,
TotalPoints = g.Sum(x => CalculateGrade(Convert.ToDouble(x.RecievedPoints),
x.MaxPoints,
Convert.ToDouble(x.Weightage)))
})
.ToList();
它将对组中的每个元素调用 CalculateGrade
方法,并将返回的值求和到 TotalPoints
属性中.
It will call CalculateGrade
method on every element from group and sum returned values into TotalPoints
property.
或者您可以更改您的 CalculateGrade
以采用 IEnumerabale<经销商>
:
Or you can change your CalculateGrade
to take IEnumerabale<Dealers>
:
private double CalculateGrade(IEnumerable<Dealers> dealers)
{
// calculations here
return dealers.Sum(x => CalculateGrade(Convert.ToDouble(x.RecievedPoints),
x.MaxPoints,
Convert.ToDouble(x.Weightage)))
}
并在查询中使用它:
return gradeData
.GroupBy(row => row.Name)
.Select(g => new Dealers
{
Name = g.Key.Name,
TotalPoints = CalculateGrade(g)
})
.ToList();
这篇关于如何在C#中将Lambda表达式作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!