What is the equivalent DISTINCT(sql server) in the Linq -
i have table(send) columns(id, userid,senddate) , table(receive) columns(id,sendid,username).
i want show records in sendtable recieveusername.
for example.
(send) 1 1 2013 2 2 2013 (recieve) 1 1 jack 2 1 ema 3 2 alex 4 2 sara result 1 1 2013 jack, ema 2 2 2013 alex, sara
i use query in sqlserver
(the distinct keyword eliminates duplicate rows results of select statement)
select distinct c2.id, (select str( username )+ ',' dbo.reciver c1 c1.sendid = c2.id xml path('')) concatenated, c2.senddate, c2.userid dbo.send c2 inner join dbo.reciver on c2.id = dbo.reciver.sendid
how query in linq?
it doesn't seem me need use distinct in linq query. assuming have relationships between tables set on linq datacontext, can this:
var result = s in context.send select new { id = s.id, userid = s.userid, date = s.senddate, users = s.receive.select(u => u.username) }
note: users
ienumerable<string>
- can use string.join()
on client join names string.
update
to return users string first need 'switch' linq objects calling asenumerable()
or tolist()
, linq sql query.
var output = s in result.asenumerable() select new { id = s.id, userid = s.userid, date = s.date, users = string.join(", ", s.users) }
also see gert arnolds answer explanation.
Comments
Post a Comment