Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,

I want to group by on multiple columns in linq.

Below is sql query I have I ant to generate similar type of group by in my linq

SQL
SELECT p.ProductID,p.Name, p.Code,st.Month,st.Year,st.StartDate,SUM(ISNULL(st.Qty,0)) 
	AS Qty, sum(ISNULL(st.Value,0)) AS Value
	FROM UserToProduct up
	INNER JOIN Product p ON up.ProductID = p.ProductID
	LEFT OUTER JOIN SalesTarget st ON p.ProductID = st.ProductID AND st.IsInActive = 0
	AND st.UserID = @UserID AND st.Year = @Year
	WHERE up.IsInActive = 0 AND p.IsInActive = 0 AND up.UserID = @UserID AND p.IsLiterature = 0
	GROUP BY p.ProductID,p.Name, p.Code,st.Month,st.Year,st.StartDate


What I have tried:

C#
var result = from up in db.UserToProducts.Where(up => up.IsInActive == false && up.UserID == userID)
                       join p in db.Products.Where(p => p.IsInActive == false && p.IsLiterature == false)
                       on up.ProductID equals p.ProductID
                       join s in db.Sales.Where(s => s.IsInActive == false && s.UserID == userID
                       && s.Date.Year == year && s.Date.Month == month)
                       on p.ProductID equals s.ProductID into joined
                       from j in joined.DefaultIfEmpty()
                       select new SalesachievementBO
                       {

                       };
Posted
Updated 3-Mar-16 10:33am
Comments
CHill60 3-Mar-16 6:58am    
And what went wrong with the code you tried?
Torakami 3-Mar-16 7:08am    
I am not getting how to do group by in my query , i have created till the point except not sure how to use multiple group by on this type of query

C#
var result = from p in db.Products.Where(p => p.IsInActive == false && p.IsLiterature == false)
                         join up in db.UserToProducts.Where(up => up.IsInActive == false
                         && up.UserID == userID) on p.ProductID equals up.ProductID
                         join ul in db.UserToLocations.Where(ul => ul.IsInActive == false)
                         on up.UserID equals ul.UserID
                         join l in db.Locations.Where(l => l.IsInActive == false)
                         on ul.LocationID equals l.LocationID
                         join s in db.Sales.Where(s => s.IsInActive == false && s.UserID == userID && s.Date.Month == month
                         && s.Date.Year == year && s.WholeSalerID == wholeSalerId) on l.LocationID equals s.LocationID into joined
                         from merged in joined.DefaultIfEmpty()
                         group new { up.Product, ul.Location, merged }
                         by new
                         {
                             LocationID = ul.Location.LocationID,
                             ul.Location.Name,
                             ProductID = up.Product.ProductID,
                             ProdictName = up.Product.Name,
                             up.Product.Code,
                             month = merged.Date.Month,
                             year = merged.Date.Year,
                             merged.Date
                         } into g
                         select new SalesachievementBO()
                         {
                             ProductID = g.Key.ProductID,
                             ProductName = g.Key.ProdictName,
                             ProductCode = g.Key.Code,
                             LocationId = g.Key.LocationID,
                             LocationName = g.Key.Name,
                             Qty = g.Sum(p => (p.merged.Qty == null ? 0 : p.merged.Qty)),
                             Price = g.Sum(p => p.merged.Price == null ? 0 : p.merged.Price)
                         };
 
Share this answer
 
Comments
Maciej Los 4-Mar-16 1:52am    
Have a 5 as an incentive to further work.
Cheers,
Maciej
All you need to do is to use GroupBy statement + Select, similar to below example:
C#
var query = dataContext.GroupBy(x=>new {A = x.Field1, B= x.Field2, C = x.Field3)
                       .Select(grp=> new {
                            Field1 = grp.Key.A,
                            Field2Sum = grp.Sum(a=>a.B)}):


Have a look here:
How to: Group Query Results (C# Programming Guide)[^]
101 LINQ Samples in C#[^]
 
Share this answer
 

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