0

I am new to linq query in Asp .Net. I want total salary for department wise using group by concept. I want result using linq query so please help me friends. I have taken one table see below. Can you please give a solution for my query? I had tried but I didn't find out the solution.

My data table is:

    id   name     dept   sal
     1   ABC      it    10000
     2   BBC      it    20000
     3   CCA      hr    30000
     4   DDA      hr    40000
     5   MMN      admin 50000

Result like:

       dept   sal  
      ----------------- 
       It    30000
       Hr    70000
       Admin 50000   
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
user1858830
  • 1
  • 1
  • 2

4 Answers4

2

Hi it should not be that difficult to get what you want.

tablename.GroupBy(g => g.dept)
.Select(s => new {
       dept = s.Key,
       sal = s.Sum(t => t.sal)
       })
.ToList();

And this gives you a list of total salary for each department

Raphael
  • 1,677
  • 1
  • 15
  • 23
0

somethink like this:

context.table.GroupBy(t => t.dept, e => e.sal).Select(r => new { key = r.Key, sum = r.Sum(x => x) });

Edit:

For using linq with dataTable add a reference to System.Data.DataSetExtensions and add System.Data namespace and call AsEnumerable() to your dataTable. Also take a look at Here

Community
  • 1
  • 1
MRB
  • 3,752
  • 4
  • 30
  • 44
0

you can do something like this ..

var list = table.GroupBy(p=>p.dept,q=>q.sal).Select(x=>new { dept = x.Key, sal = x.Sum(s=>s.sal)});
Sandeep Chauhan
  • 1,313
  • 1
  • 10
  • 23
-1
        List<Student> studentsList = new List<Student>()
        {
           new Student(){Id = 1,Name ="krish",Gender="male",City="ahmedabad",Salary=2750},
           new Student(){Id = 2,Name ="rahul",Gender="male",City="delhi",Salary=1000},
           new Student(){Id = 3,Name ="neha",Gender="female",City="surat",Salary=2500},
           new Student(){Id = 4,Name ="dev",Gender="male",City="mumbai",Salary=7800},
           new Student(){Id = 5,Name ="priya",Gender="female",City="ahmedabad",Salary=5600},
        };

        var queryList = from SL in studentsList group SL by SL.Gender into GROUPGENDER orderby GROUPGENDER.Key select GROUPGENDER;
        foreach(var studentList in queryList)
        {
            Console.WriteLine(">>>>>group by<<<<<");
            Console.WriteLine(studentList.Key);
            Console.WriteLine(">>>>>group by<<<<<");
            foreach (var item in studentList)
            {
                Console.WriteLine(item.Id);
                Console.WriteLine(item.Name);
                Console.WriteLine(item.Gender);
                Console.WriteLine(item.City);
            }                    
        }                     
        Console.Read();
  • 1
    Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.[How to Answer](https://stackoverflow.com/help/how-to-answer) – Elletlar Oct 02 '18 at 21:27