I am having a table like this :
EmployeeId EmployeeName ItemName
4 Ganesh Key Board
4 Ganesh Processor
1 Jignesh Key Board
1 Jignesh Mouse
1 Jignesh Processor
3 Rakesh Key Board
2 Tejas Key Board
2 Tejas Mouse
2 Tejas Processor
I need to query this as if the itemname is different for the same employeeid
and employeename
we should have the items as ',' separated.
Like the one which is given below :
EmployeeId EmployeeName ItemName
1 Jignesh Key Board, Mouse, Processor
2 Tejas Key Board, Mouse, Processor
3 Rakesh Key Board
4 Ganesh Key Board, Processor
Here is the SQL Query for this:
Could anyone help me to convert the above SQL Query into Lambda Expression?
I'm assuming by
Lambda expression
you mean a Linq statement (e.g. to EF or Linq2Sql).The
FOR XML PATH
andSTUFF
example shown was a hack to workaround the lack ofGROUP_CONCAT
orLISTAGG
in Sql Server. Finally in Sql 2017 there is STRING_AGGYou don't need to reproduce the hack at all in LINQ - instead, simply load all rows for the set of employees of interest into memory,
GroupBy
the required key, and then useString.Join
in a select projection:Where
employeeItems
is a projection of the join betweenEmployee
andItems
:Result: