tags:

views:

30

answers:

2

I have crawled over several of the various questions on Linq-to-SQL dealing with joins, counts, sums and etc.. But I am just having a difficult time grasping how to do the following simple SQL Statement...

SELECT     
   tblTechItems.ItemName, 
   SUM(tblTechInventory.EntityTypeUID) AS TotalOfItemByType
FROM         
   tblTechInventory 
INNER JOIN
   tblTechItems ON tblTechInventory.ItemUID = tblTechItems.ItemUID
GROUP BY 
   tblTechInventory.StatusUID, 
   tblTechItems.ItemName, 
   tblTechInventory.InventoryUID
HAVING      
   tblTechInventory.StatusUID = 26
+1  A: 

Try this:

var query = from e in db.tblTechInventory
            join f in db.tblTechItems on e.ItemUID equals f.ItemUID into x
            from y in x
            group e by new { e.StatusUID, y.ItemName, e.InventoryUID } into g
            where e.StatusUID == 26
            select new {
                g.Key.ItemName,
                TotalOfItemByType = g.Sum(e => e.EntityTypeUID)
            };
Jason
There is no "tblTechInventory" table mentioned there.
danijels
@danijels: My mistake; it was difficult to read his code before it was reformatted to not be on a single line. Please see my edit.
Jason
downvote removed
danijels
Thanks for your help
Warren
+1  A: 

I'll give it a shot...

var results = tblTechInventory
  .Join(tblTechItems, i=> i.ItemUID, o => o.ItemUID, (i,o) => new {i.ItemName, o.EntityTypeUID, o.StatusUID, i.ItemName, o.InventoryUID})
  .Where(o => o.StatusUID == 26)
  .GroupBy(g => new {g.StatusUID, g.ItemName, g.InventoryUID}, (gr, items) => new {gr.Key.ItemName, items.Sum(i => i.EntityTypeUID)});
danijels
I wish whoever downvoted would give a clue as to why...
danijels