score:1
Try this:
var vGroup = from p in ClassRoom
group p by new { p.ClassRoomNo, p.TeacherName }
into g
from i in g
select new
{
i.ClassRoomNo,
i.TeacherName,
i.ClassRoomTitle,
NoSessionsPerTeacher = g.Count()
};
var pGroup = from p in vGroup
group p by new { p.ClassRoomNo }
into g
from i in g
select new
{
i.ClassRoomTitle,
NoSessionsPerRoom = g.Count(),
i.TeacherName,
i.NoSessionsPerTeacher
};
var result = pGroup.OrderBy(p => p.ClassRoomNo).ThenBy(p => p.TeacherName);
I didn't test the above but you can check my original code in case I got something wrong in the rewrite:
var vGroup = from p in Products
group p by new { p.ProductId, p.VariantId }
into g
from i in g
select new
{
i.ProductId,
i.VariantId,
VariantCount = g.Count()
};
var pGroup = from p in vGroup
group p by new { p.ProductId }
into g
from i in g
select new
{
i.ProductId,
ProductCount = g.Count(),
i.VariantId,
i.VariantCount
};
var result = pGroup.OrderBy(p => p.ProductId).ThenBy(p => p.VariantId);
score:1
var classRooms = from c in context.ClassRooms
group c by new {c.ClassRoomNo} into room
select new {
Title = room.First().ClassRoomTitle,
NoSessions = room.Count(),
Teachers = from cr in room
group cr by new {cr.TeacherName} into t
select new {
Teacher = t.Key,
NoSessions = t.Count()
}
};
A bit more structured than the posted expected result, but I find that to be better.
You can always use SelectMany if you want to go back to unstructured:
var unstructured = classRooms
.SelectMany(c=> c.Teachers.Select( t=> new {
Title = c.Title,
SessionsPerRoom = c.NoSessions,
Teacher = t.Teacher,
SessionsPerTeacher = t.NoSessions
});
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- Can do rank over partition in SQL, how to do in LINQ to xml
- creating Linq to sqlite dbml from DbLinq source code
- translate first_value over partition by to linq
- LINQ (to Oracle) - Row_Number() Over Partition By
- How to get PEX to automatically generate inputs for code involving LINQ
- source code for LINQ 101 samples
- Loop over parameters to generate LINQ at runtime
- Performant running SUM OVER Partition in LINQ
- Is there an equivalent in LINQ for the SQL Server PARTITION OVER functionality?
- translate row_number over partition to C# linq
- Convert Over Partition to LINQ
- row_number() over partition in linq
- Linq OrderBy in select if count is over 0
- Linq - Group by and count over multiple fields (output on single total)
- c# Linq or code to extract groups from a single list of source data
- Generate linq query to get count of distinct records in single column
- Convert string[] to int[] in one line of code using LINQ
- LINQ with groupby and count
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ - Left Join, Group By, and Count
- Linq with group by having count
- Linq code to select one item
- How to Count Duplicates in List with LINQ
- Using GroupBy, Count and Sum in LINQ Lambda Expressions
- Linq distinct - Count
- How are people unit testing code that uses Linq to SQL
- LINQ Partition List into Lists of 8 members
- Dynamically generate LINQ queries
- SELECT COUNT in LINQ to SQL C#
- Comparing two anonymous type arrays and find element Indexes [VB.net]
- LINQ identity function
- Entity Framework Core: Include many-to-many related objects in WebAPI
- Linq Group result to an Object
- Linq Nhibernate left join
- How to get specific columnar maximums in List of arrays?
- Search a CheckedListBox that is bounded to a datatable using linq
- LINQ to Entities does not recognize the method (Trying to get Previous and Next record)
- Handling large SQL queries with LINQ
- How to get the last value from a model list without looping it in MVC?
- SqlDateTime overflow when inserting value with DateTime.Now
- Use Linq to get the last order placed yesterday for each customer
- LinqToSQL recursive select
- linq order by based on multiple columns and various crietria
- How to select only specific properties of navigation property
- LINQ Include vs Join. Are they equivalent?
- Generic LINQ query predicate?
- What is difference between System.Linq.Enumerable.WhereListIterator & System.Linq.Enumerable.WhereSelectListIterator?
- Why I see so much code duplication in Linq in Reflector?
- Save Entity Framework Linq Query to database