Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I cannot return my query when I group data. Please check my issue

public IQueryable<GroupModel> GetAll()
        {
            List<Category> listCategories = new List<Category>
            {
                new Category {Id = 1, CateName = "SmartPhone", Description = "aaaaaa"},
                new Category {Id = 2, CateName = "Laptop", Description = "bbbb"},
                new Category {Id = 3, CateName = "Desktop", Description = "ccccc"},
            };

            List<Product> listProducts = new List<Product>
            {
                new Product {Id = 1, ProdName = "Lumia 720", CategoryId = 1, ColorId = 2},
                new Product {Id = 2, ProdName = "PC HP", CategoryId = 3, ColorId = 1},
                new Product {Id = 3, ProdName = "PC Dell", CategoryId = 3, ColorId = 1},
                new Product {Id = 4, ProdName = "Laptop Lenovo", CategoryId = 2, ColorId = 2},
                new Product {Id = 5, ProdName = "Lumia 920", CategoryId = 1, ColorId = 2},
                new Product {Id = 6, ProdName = "Laptop Dell", CategoryId = 2, ColorId = 3},
                new Product {Id = 7, ProdName = "Laptop HP", CategoryId = 2, ColorId = 3},
                new Product {Id = 7, ProdName = "Lumia 1020", CategoryId = 1, ColorId = 1}
            };

            var query = (from p in listProducts
                         join co in listColors on p.ColorId equals co.ColorId
                         join c in listCategories on p.CategoryId equals c.Id into e
                         from j in e.DefaultIfEmpty()
                         
                         select new GroupModel
                         {
                             CategoryId = j.Id,
                             CategoryName = j.CateName,
                             ProductId = p.Id,
                             ProductName = p.ProdName,
                             ColorId = co.ColorId,
                             ColorName = co.ColorName

                         }).ToList().GroupBy(x => x.CategoryName);



            return query.AsQueryable();
        }
Posted

1 solution

The result of your query is of type IGrouping<string, GroupModel> where the Key is the string. You can use simple foreach loops to extract the data you require.


C#
List<GroupModel> groupModels= new List<GroupModel>();
         foreach (IGrouping<string, GroupModel> groupings in query)
         {
             foreach (GroupModel gm in groupings)
             {
                 groupModels.Add(gm);
             }
         }
 
Share this answer
 
v2
Comments
Khiem Dev 8-Jan-15 22:12pm    
In my razor view how do I foreach ?
George Swan 9-Jan-15 1:20am    
I suggest that you add the above code to your method and return the GroupModel list instead of the IQueryable. Best wishes.

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