how to group by multiple columns using linq

rlcrews

I have a database table with a dataset that contains multiple rows of data as follows

ItemId               Code                             StatusId
-------------------- ---------------------------------------------
62224                NC0860000                             8
62225                NC0860000                             8
62226                NC0860000                             8
62227                NC0860200                             5
62228                NC0860000                             5
62229                NC0860000                             5
62230                NC0860000                             5

What I would like to accomplish is an output result as

NC0860000  8  3  (code, status, count)
NC0860000  5  3

I don't fully understand how grouping works in EF. I can get the key and a count of a single group using a query as:

var results = (from ssi in ctx.StageSubmitItems
                           join s in ctx.StageSubmissions on ssi.SubmissionId equals s.SubmissionId
                           where s.ContributorCode == contributorId
                           group ssi.SubmitItemId by ssi.AgencyCode into g
                           select new {AgencyCode = g.Key, Count = g.Count() }).ToList();

But I can't figure out how to group by code and then by StatusId and then produce a count of the total number of rows by status.

I'd appreciate any suggestions on where to look on how to accomplish this or what I am doing incorrectly in the query.

Timothy Walters

You can group by a new anon class as follows:

// I created a Foo class to show this working
var fooList = new List<Foo> {
    new Foo { ItemId = 62224, Code = "NC0860000", StatusId = 8 },
    new Foo { ItemId = 62225, Code = "NC0860000", StatusId = 8 },
    new Foo { ItemId = 62226, Code = "NC0860000", StatusId = 8 },
    new Foo { ItemId = 62227, Code = "NC0860200", StatusId = 5 },
    new Foo { ItemId = 62228, Code = "NC0860000", StatusId = 5 },
    new Foo { ItemId = 62229, Code = "NC0860000", StatusId = 5 },
    new Foo { ItemId = 62230, Code = "NC0860000", StatusId = 5 },
};

var results = (from ssi in fooList
    // here I choose each field I want to group by
    group ssi by new { ssi.Code, ssi.StatusId } into g
    select new { AgencyCode = g.Key.Code, Status = g.Key.StatusId, Count = g.Count() }
).ToList();

// LINQPad output command
results.Dump();

With the data provided, here is the output:

AgencyCode Status Count
NC0860000  8      3 
NC0860200  5      1 
NC0860000  5      3 

I am guessing "NC0860200" is an error, but it is in your sample data so I included it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to group by multiple columns using linq

From Dev

Group & Aggregate multiple columns using LINQ

From Dev

How to group by on multiple columns from datatable with linq?

From Dev

How to group by on multiple columns from datatable with linq?

From Dev

Linq Group By Multiple Columns

From Dev

Multiple Tables Group and substract sum of columns using linq sql

From Dev

LINQ - group/sum multiple columns

From Dev

LINQ - group/sum multiple columns

From Java

Using group by on multiple columns

From Dev

How to Group By on Columns in datatable in Linq

From Dev

How to use group by for multiple tables using multiple columns

From Dev

LINQ Group By using multiple criteria

From Dev

SQL to Linq select multiple columns in group by

From Dev

Group by multiple columns linq count nested row

From Dev

LINQ query string[] group by multiple anonymous columns

From Dev

Group by with multiple and nullable columns, C# Linq?

From Dev

How do I write an exists subquery using multiple columns in LINQ?

From Dev

how to equals between multiple table columns in join using Linq

From Dev

How to do a group by count using multiple columns in R?

From Dev

How to select multiple columns using sum projection with group by in Hibernate

From Dev

How to select multiple columns using sum projection with group by in Hibernate

From Dev

Update Multiple columns of multiple rows using LINQ

From Dev

Update Multiple columns of multiple rows using LINQ

From Dev

How to use group by on multiple columns?

From Dev

how to group multiple columns in sql

From Dev

Group by multiple columns using multiple keys

From Dev

how to group by in LINQ and not select all columns

From Dev

how to group by in LINQ and not select all columns

From Dev

Join Multiple Datatables in LINQ using Group Join

Related Related

  1. 1

    how to group by multiple columns using linq

  2. 2

    Group & Aggregate multiple columns using LINQ

  3. 3

    How to group by on multiple columns from datatable with linq?

  4. 4

    How to group by on multiple columns from datatable with linq?

  5. 5

    Linq Group By Multiple Columns

  6. 6

    Multiple Tables Group and substract sum of columns using linq sql

  7. 7

    LINQ - group/sum multiple columns

  8. 8

    LINQ - group/sum multiple columns

  9. 9

    Using group by on multiple columns

  10. 10

    How to Group By on Columns in datatable in Linq

  11. 11

    How to use group by for multiple tables using multiple columns

  12. 12

    LINQ Group By using multiple criteria

  13. 13

    SQL to Linq select multiple columns in group by

  14. 14

    Group by multiple columns linq count nested row

  15. 15

    LINQ query string[] group by multiple anonymous columns

  16. 16

    Group by with multiple and nullable columns, C# Linq?

  17. 17

    How do I write an exists subquery using multiple columns in LINQ?

  18. 18

    how to equals between multiple table columns in join using Linq

  19. 19

    How to do a group by count using multiple columns in R?

  20. 20

    How to select multiple columns using sum projection with group by in Hibernate

  21. 21

    How to select multiple columns using sum projection with group by in Hibernate

  22. 22

    Update Multiple columns of multiple rows using LINQ

  23. 23

    Update Multiple columns of multiple rows using LINQ

  24. 24

    How to use group by on multiple columns?

  25. 25

    how to group multiple columns in sql

  26. 26

    Group by multiple columns using multiple keys

  27. 27

    how to group by in LINQ and not select all columns

  28. 28

    how to group by in LINQ and not select all columns

  29. 29

    Join Multiple Datatables in LINQ using Group Join

HotTag

Archive