1

I have class like this

public class Student
{

    public int Id { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public GRADE Grade { get; set; }
    public string Nationality { get; set; }
}
public enum GRADE
{
    A = 0,
    B = 1,
    C = 2,
    D = 3,
    E = 4
}

var list = new List<Student>();
list.Add(new Student() { Id = 1, Name = "Prasad", Gender = "M", Nationality = "India", Grade = GRADE.A });
list.Add(new Student() { Id = 2, Name = "Raja", Gender = "M", Nationality = "India", Grade = GRADE.B });
list.Add(new Student() { Id = 3, Name = "Hindu", Gender = "F", Nationality = "India", Grade = GRADE.A });
list.Add(new Student() { Id = 4, Name = "Hamed", Gender = "M", Nationality = "India", Grade = GRADE.C });
list.Add(new Student() { Id = 5, Name = "Priya", Gender = "F", Nationality = "India", Grade = GRADE.D });
list.Add(new Student() { Id = 6, Name = "Meera", Gender = "F", Nationality = "India", Grade = GRADE.B });

I got the solution like this, For each expression i want to write bunch of code.. Sum,Avg,Count etc

Linq Expressions

//count
var c = (from x in list.GroupBy(k => k.Gender)
         select new
         {
             category = x.Key,
             Value = x.Count()
         }).ToList();

//sum
var s = (from x in list.GroupBy(k => k.Gender)
         select new
         {
             category = x.Key,
             Value = x.Sum(k => (int)k.Grade)
         }).ToList();

//avg
var a = (from x in list.GroupBy(k => k.Gender)
         select new
         {
             category = x.Key,
             Value = x.Average(k => (int)k.Grade)
         }).ToList();

I am trying to make one function, based on the aggregate function; it should return the value, I tried I could not find it.

6
  • what about putting each linq expression inside a method, and then create a new method with switch statement, which will be called based on methods parameter? Commented May 22, 2016 at 7:19
  • i tried already with switch statement inside the expression, that's not helful.. Commented May 22, 2016 at 7:20
  • It's not clear to me what you're trying to achieve - what "one function" do you mean? Could you at least post how you're expecting to use this function, if the problem is just the implementation? Commented May 22, 2016 at 7:22
  • See for an aggregate expression, i written three linq expression to get the data... were i would like to have a static function, based on the aggregate it should return... Commented May 22, 2016 at 7:27
  • Prasad its not clear, I think either you are trying to create one aggregate function that's return all sum/cout/avg. values or either one to one means one aggregate function for one function(i.e. sum). Commented May 22, 2016 at 7:28

2 Answers 2

2

One issue you have is that all three aggregates do not have the same return type, also if you use a function then the return type would have to be object because you are returning an anonymous type.

The closest I could get to what I think you want was this;

Step 1: create a new type;

public class AggregateValue<T>
{
    public string Category { get; set; }
    public T Value { get; set; }
}

Step 2: Create a function that returns a collection of this type and accepts a Func as a parameter that will calculate your different aggregates;

    IEnumerable<AggregateValue<T>> GetAggregateValues<T>(List<Student> students, Func<IEnumerable<Student>, T> aggregateFunction)
    {
        return (from x in students.GroupBy(k => k.Gender)
                 select new AggregateValue<T>
                 {
                     Category = x.Key,
                     Value = aggregateFunction(x)
                 }).ToList();
    }

You can use it like this;

        var list = new List<Student>();
        list.Add(new Student() { Id = 1, Name = "Prasad", Gender = "M", Nationality = "India", Grade = GRADE.A });
        list.Add(new Student() { Id = 2, Name = "Raja", Gender = "M", Nationality = "India", Grade = GRADE.B });
        list.Add(new Student() { Id = 3, Name = "Hindu", Gender = "F", Nationality = "India", Grade = GRADE.A });
        list.Add(new Student() { Id = 4, Name = "Hamed", Gender = "M", Nationality = "India", Grade = GRADE.C });
        list.Add(new Student() { Id = 5, Name = "Priya", Gender = "F", Nationality = "India", Grade = GRADE.D });
        list.Add(new Student() { Id = 6, Name = "Meera", Gender = "F", Nationality = "India", Grade = GRADE.B });

        var sumGrades = new Func<IEnumerable<Student>, int>(p => p.Sum(l => (int)l.Grade));
        var aveGrades = new Func<IEnumerable<Student>, double>(p => p.Average(k => (int)k.Grade));
        var count = new Func<IEnumerable<Student>, int>(p => p.Count());

        var c = GetAggregateValues(list, count);
        var s = GetAggregateValues(list, sumGrades);
        var a = GetAggregateValues(list, aveGrades);
Sign up to request clarification or add additional context in comments.

2 Comments

You should make AggregateValue a generic type of AggregateValue<T> and this then would be a nice solution.
I have edited my answer making AggregateValue a generic type, thank you to Enigmativity for the suggestion
2

You can combine all your aggregations in one statement:

var result = (from x in list.GroupBy(k => k.Gender)
    select new
    {
        category = x.Key,
        Count = x.Count(),
        Sum = x.Sum(k => (int)k.Grade),
        Average = x.Average(k => (int)k.Grade)
    }).ToList();

4 Comments

Dont want to return all the aggregate expression in same expression..i would like to have a static function, based on the aggregate function it should return the value
@PrasadRaja You can't do that, providing part of Linq expression as a parameter is more complicated.
@PrasadRaja - What is "the value"?
exampel Value = x.Sum(k => (int)k.Grade) , Value = x.Count() , Value = x.Average(k => (int)k.Grade)

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.