I have a linq query that I use to get data from 3 tables using 2 joins. I would prefer to write this as a lambda expression so my team finds it more readable. Is there a way to do this though? I can't find any decent examples.
var q = (
from sus in susManager.Get()
join su in suManager.Get() on sus.SUId equals su.Id
join p in pManager.Get() on su.PId equals p.Id
where sus.EndTimeStamp >= oneDayAgo
select new
{
Name = p.FirstName + " " + p.LastName,
Email = su.Email,
LastLogIn = sus.StartTimeStamp,
LastSessionDurationInMinutes =
DbFunctions.DiffMinutes(sus.StartTimeStamp, sus.EndTimeStamp),
LastActive = sus.EndTimeStamp
}).ToList();
The lambda equivalent would look something like this, assuming that the Get()
functions return List
s:
var q = systemUserSessionManager.Get()
.Where(sus => sus.EndTimeStamp >= oneDayAgo)
.Join(systemUserManager.Get(),
sus => sus.SystemUserId,
su => su.Id,
(sus, su) => new { sus, su })
.Join(personManager.Get(),
j => j.su.PersonId,
p => p.Id,
(j, p) => new { sus = j.sus, su = j.su, p })
.Select(x => new
{
Name = p.FirstName + " " + p.LastName,
Email = su.Email,
LastLogIn = sus.StartTimeStamp,
LastSessionDurationInMinutes =
DbFunctions.DiffMinutes(sus.StartTimeStamp, sus.EndTimeStamp),
LastActive = sus.EndTimeStamp
})
.ToList();
Try something like this
var q = (
from sus in systemUserSessionManager.Get()
join su in systemUserManager.Get() on sus.SystemUserId equals su.Id
join p in personManager.Get() on su.PersonId equals p.Id
select new { sus = sus, su = su, p = p})
.Where(x => x.sus.EndTimeStamp >= oneDayAgo)
.Select(x => new {
Name = x.p.FirstName + " " + x.p.LastName,
Email = x.su.Email,
LastLogIn = x.sus.StartTimeStamp,
LastSessionDurationInMinutes =
DbFunctions.DiffMinutes(x.sus.StartTimeStamp, x.sus.EndTimeStamp),
LastActive = x.sus.EndTimeStamp
}).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.