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

ID = 1, Group = Auto/Loan
ID = 2, Group = Auto/Repair
ID = 3, Group = Boat/Fuel


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

Who is Participating?
Bob LearnedConnect With a Mentor Commented:
At the point of the group by, you can only access elements of "g", and not "categoryRef".  What does g.Value give you?  It should be anonymous type from the let statement.  

My guess is g.Value.categoryRef.Name + "/" + g.Value.categorySubRef.Name
WorknHardrAuthor Commented:
Your guess is close, I'm using this one now;

select new {
CategoryAsgnID = g.FirstOrDefault().categoryAsgn.CategoryAsgnID,
GroupName = g.FirstOrDefault().categoryRef.Name
 + "/" +
WorknHardrAuthor Commented:
Thx for sending down the right path
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.