3

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.

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;
}

Can somebody advise how to pass parameters in this , or how to pass lamda expressions and calculate grade?

Many thanks in advance...

3
  • This: .GroupBy(row => new { row.Name }) can be written as .GroupBy(row => row.Name), no new necessary. Commented Sep 12, 2013 at 10:01
  • if I remove new, I am getting exception - Error 31 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement C:\Users\DEV7\Documents\csdev\trunk\DWIZA\App_Code\Support\AngieStats.cs 497 35 C:\...\DWIZA\ Commented Sep 12, 2013 at 10:06
  • If you make the replace, you have to change the g.Key.Name to g.Key Commented Sep 12, 2013 at 10:13

2 Answers 2

5

Looks like you need that:

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();

It will call CalculateGrade method on every element from group and sum returned values into TotalPoints property.

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)))
}

And use it in your query:

return gradeData
    .GroupBy(row => row.Name)
    .Select(g => new Dealers
    {
        Name = g.Key.Name,
        TotalPoints = CalculateGrade(g)
    })
    .ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

how can I handle calculation in CalculateGrade() if I pass enumerable dealers?
You can use LINQ as well.
3

This doesn't solve your problem, but it gives you an overview how to send lambdas into methods

You would use Func & Action to pass lambdas into a method

Func

Can have 1 - 15 input parameters and must have an output parameter

Action

Can have 1 - 16 input parameters with no output parameter

ie, how I imagine they do it in EntityFramework for a where predicate

public static List<People> Filter<TEntity>(this List<People> myList, Func<TEntity, TResult> predicate)
{
    return myList.Where(predicate).ToList();
}

the usage would then be something like

myList.Filter(ml => ml.Age > 18);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.