0

我有以下查询,它返回了一个客人列表以及一个 RSVP 列表。但是,由于每个事件用户可以有多个 RSVP,我只想获得最新的。RSVPs 表有一个提交日期列。

from guest in Guests
                join rsvp in RSVPs.Where(o => o.EventID == "1234")
                on guest.UserName equals rsvp.UserName into sr
                from x in sr.DefaultIfEmpty()
                select new
                {
                     guest,
                    x
                }
4

3 回答 3

0

按日期排序并取 1:

(from guest in Guests
            join rsvp in RSVPs.Where(o => o.EventID == "1234")
            on guest.UserName equals rsvp.UserName into sr
            from x in sr.DefaultIfEmpty()
            orderby x.RSVPDate desc
            select new
            {
                 guest,
                x
            })
.Take(1);
于 2013-03-06T01:51:08.503 回答
0
from guest in Guests
                join rsvp in RSVPs.Where(o => o.EventID == "1234")
                on guest.UserName equals rsvp.UserName into sr
                from x in sr.DefaultIfEmpty()
                select new
                {
                     guest,
                    guest.RSVPs.Where(o => o.EventID =="1234")
                               .OrderByDesc(o=>o.SubmittedDate)
                               .FirstOrDefault()
                }

事实上,你也可以试试这个,想法是加载正确的 RSVP 并加载附加的来宾对象,假设存在外键关系。

context.RSVPs.Include("Guest").Where(o => o.EventID =="1234").OrderByDesc(o=>o.SubmittedDate).FirstOrDefault()

否则这可能是一个选项:

(from rsvp in context.RSVP
where rsvp.EventId == "1234"
join guest in context.Guests on rsvp.UserName equals guest.UserName 
select new {rsvp, guest}
orderby desc rsvp.SubmittedDate).FirstOrDefault()
于 2013-03-12T16:42:14.737 回答
0

伪——

group x by username into g
select new {
  UserName = g.key // username
  FirstRsvp = g.OrderByDescending(item => item.RsvpDate).FirstOrDefault()
}

希望这是 LINQ to objects,因为这种事情可能没有在 SQL 提供程序中定义。如果是 SQL 并且没有实现,.AsEnumerable() 会将查询拉入内存,然后从那里开始是 L2O。

于 2013-03-06T02:15:47.553 回答