Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

LINQ Groub By and Return Alias ID and GroupName as Concat Data?

Difficult title indeed. I had the data returning great until including the Group By.  I need to combine two columns data like so:

         ID = g.Key,
         Group = categoryRef.Name + "/" + categorySubRef.Name

[Output]
ID = 1, Group = Auto/Loan
ID = 2, Group = Auto/Repair
ID = 3, Group = Boat/Fuel
etc...

[code]   

var groups = 
(from c in context.CategoryAsgns
 join r in context.CategoryRefs on c.CategoryID equals r.CategoryID
 join r2 in context.CategorySubRefs on c.CategorySubID equals r2.CategorySubID
 let j = new { categoryAsgn = c, categoryRef = r, categorySubRef = r2 }
 orderby j.categoryRef.Name ascending
 group j by j.categoryRef.Name into g
 select new { ID = g.Key, 
Group = categoryRef.Name +"/"+ categorySubRef.Name) }).ToList();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WorknHardr
WorknHardr

ASKER

Your guess is close, I'm using this one now;

select new {
CategoryAsgnID = g.FirstOrDefault().categoryAsgn.CategoryAsgnID,
GroupName = g.FirstOrDefault().categoryRef.Name
 + "/" +
g.FirstOrDefault().categorySubRef.Name
 }).ToList();
Thx for sending down the right path