Skip to main content
fixed sample code
Source Link
Geoffrey
  • 976
  • 1
  • 10
  • 16

In my opinion, using Fluent Syntax usually makes it a bit easier to understand what is going wrong here.

As soon as you group your data you are no longer working on the individual objects, but on a 'group', which is the key and an enumerable of objects.

Getting the sum per employee should then be grouping by the full employee id and then parsing the MNTH1 fields of your objects and summing them.

dataGridView1.DataSource = movie2 .GroupBy(p => p.EMPLID) // create a group of data per employee .Select(g => new { EMPLID = g.Key, // the employee id is the group key Sum = g.SelectSum(data => decimal.Parse(data.MNTH1)).Sum() // the total amount of hours isparse theand sum   of parsed MNTH1 data}) }.ToArray(); 

Edit: you are right, you need the ToArray to evaluate the query. I just verified on my computer and it works.

In my opinion, using Fluent Syntax usually makes it a bit easier to understand what is going wrong here.

As soon as you group your data you are no longer working on the individual objects, but on a 'group', which is the key and an enumerable of objects.

Getting the sum per employee should then be grouping by the full employee id and then parsing the MNTH1 fields of your objects and summing them.

dataGridView1.DataSource = movie2 .GroupBy(p => p.EMPLID) // create a group of data per employee .Select(g => new { EMPLID = g.Key, // the employee id is the group key Sum = g.Select(data => decimal.Parse(data.MNTH1)).Sum() // the total amount of hours is the sum of parsed MNTH1 data }); 

In my opinion, using Fluent Syntax usually makes it a bit easier to understand what is going wrong here.

As soon as you group your data you are no longer working on the individual objects, but on a 'group', which is the key and an enumerable of objects.

Getting the sum per employee should then be grouping by the full employee id and then parsing the MNTH1 fields of your objects and summing them.

dataGridView1.DataSource = movie2 .GroupBy(p => p.EMPLID) // create a group of data per employee .Select(g => new { EMPLID = g.Key, // the employee id is the group key Sum = g.Sum(data => decimal.Parse(data.MNTH1)) // parse and sum   }) .ToArray(); 

Edit: you are right, you need the ToArray to evaluate the query. I just verified on my computer and it works.

Source Link
Geoffrey
  • 976
  • 1
  • 10
  • 16

In my opinion, using Fluent Syntax usually makes it a bit easier to understand what is going wrong here.

As soon as you group your data you are no longer working on the individual objects, but on a 'group', which is the key and an enumerable of objects.

Getting the sum per employee should then be grouping by the full employee id and then parsing the MNTH1 fields of your objects and summing them.

dataGridView1.DataSource = movie2 .GroupBy(p => p.EMPLID) // create a group of data per employee .Select(g => new { EMPLID = g.Key, // the employee id is the group key Sum = g.Select(data => decimal.Parse(data.MNTH1)).Sum() // the total amount of hours is the sum of parsed MNTH1 data });