Tuesday, May 7, 2013

How to create the equivalent of 'Stuff' SQL function in Linq

I was wondering how you would go about writing the following SQL as a LINQ query?



select Title, EntryMonth, stuff((
select ', ' + cast(EntryYear as varchar)
from Table1 t2
where t1.Title = t2.Title
and t1.EntryMonth = t2.EntryMonth
for xml path('')
), 1, 2, '') as Years
from table1 t1
group by Title, EntryMonth


But in addition it would also need to include an id field.


It's just that i'm confused as to how to write the equivalent of the STUFF function (i.e. inserting a string into another string). Is it possible to achieve this in a single Linq query? Any examples would be greatly appreciated.


Thanks.


No comments:

Post a Comment