Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how to get a list of distinct items and their count using linq query and return it to the controller in mvc

my database table is like
---------------------------
id name amount
1 a 10
1 a 20
2 b 30
2 b 40
2 b 50

i want the result to be
---------------------------
id name amount
1 a 30
2 b 120

and return it to the mvc controller with all parameters

What I have tried:

how to get a list of distinct items and their count using linq query and return it to the controller in mvc
Posted
Updated 18-May-16 5:20am
v2
Comments
Animesh Datta 18-May-16 8:42am    
what do you mean by "return it to the mvc controller with all parameters" ?

1 solution

try this
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
 
    class Class1
    {
        static void Main(string[] args)
        {
            List<MyEntity> lst = new List<MyEntity>();
            lst.Add(new MyEntity() { ID = 1, Name = "a", Amount = 10 });
            lst.Add(new MyEntity() { ID = 1, Name = "a", Amount = 20 });
            lst.Add(new MyEntity() { ID = 2, Name = "b", Amount = 30 });
            lst.Add(new MyEntity() { ID = 2, Name = "b", Amount = 40 });
            lst.Add(new MyEntity() { ID = 2, Name = "b", Amount = 50 });

            var lstOut = lst.GroupBy(k => new { k.ID, k.Name }).Select(x => new MyEntity() { ID = x.Key.ID, Name = x.Key.Name, Amount = x.ToList().Sum(k => k.Amount) }).ToList();
        }
    }
        

    class MyEntity
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public double Amount { get; set; }
    }
 
Share this answer
 
v3
Comments
Richard Deeming 18-May-16 13:42pm    
You don't need the inner .ToList() call - just Amount = x.Sum(k => k.Amount) will do the trick.

You could also combine the GroupBy and Select methods:
lst.GroupBy(k => new { k.ID, k.Name}, (key, items) => new MyEntity { ID = key.ID, Name = key.Name, Amount = items.Sum(k => k.Amount) }).ToList()
Karthik_Mahalingam 18-May-16 22:36pm    
yes Richard, you are right.
[no name] 19-May-16 10:59am    
is there any other simple way to do this
Karthik_Mahalingam 19-May-16 11:04am    
Linq is the simpler way, if you are using loops, the no of lines will be huge..
Karthik_Mahalingam 19-May-16 11:21am    
this ?

List<myentity> lst = new List<myentity>();
lst.Add(new MyEntity() { ID = 1, Name = "a", Amount = 10 });
lst.Add(new MyEntity() { ID = 1, Name = "a", Amount = 20 });
lst.Add(new MyEntity() { ID = 2, Name = "b", Amount = 30 });
lst.Add(new MyEntity() { ID = 2, Name = "b", Amount = 40 });
lst.Add(new MyEntity() { ID = 2, Name = "b", Amount = 50 });

int[] ids = lst.Select(k => k.ID).Distinct().ToArray();
List<myentity> lstResult = new List<myentity>();
foreach (int id in ids)
{
var temp = lst.Where(k => k.ID == id).ToList();
double sum = temp.Sum(k => k.Amount);
var item = temp[0];
item.Amount = sum;
lstResult.Add(item);

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900