2

我在 DB 中有两个要从中选择的表。在其中一个“事件”中,我有事件(Id,开始,结束,类型),在另一个中,我有“Events_before_after”,我收集与“事件”中的一些事件相关的事件 - (Id,EventId,开始,结束,输入)。
我还有一个事件 id 列表,我需要在选择查询中对事件进行排序。

这就是我所拥有的:

List<int> orderIds = ...

from order in orderIds
join event in events order on order equals event.id
select new DataEvent ()
{
    eventId = event.Id,
    start = event.start,
    end = event.end,
    type = event.type
}).Concat
from eventOther in Events_before_after
select new DataEvent ()
{
    eventId = eventOther.Id,
    start = eventOther.start,
    end = eventOther.end,
    type = eventOther.type
}

是否也可以订购第二个选择?我的意思是 - 有没有办法通过eventId哪些链接到“事件”来订购它?

我需要的是:

event1
event2
eventBefore3
event3
eventAfter3

我所拥有的是:

event1
event2
event3
eventBefore3
eventAfter3
4

2 回答 2

3

你可以这样做:

 //your query
 ...
from eventOther in Events_before_after
select new DataEvent ()
{
    eventId = eventOther.Id,
    start = eventOther.start,
    end = eventOther.end,
    type = eventOther.type
}
into NewEvent
order by NewEvent.eventId
select NewEvent;
于 2012-11-17T09:06:50.163 回答
2

如果我得到您的要点,您想将给定键上的相关事件组合在一起,对它们进行排序,然后按照给定的键顺序投影和展平它们....如果您想这样做,请尝试以下操作:

var orderedEvents = orderIds.Join(
                     //Construct Groups 
                     events.GroupBy( e => e.Id)
                           .GroupJoin( Events_before_after,
                                       g => g.Key,
                                       e => e.EventId,
                                       (gEvent, gEventBA) => new { Key = gEvent.Key,
                                       EventGroup = 
                                       gEvent.Select( e => new DataEvent ()
                                                     {
                                                      eventId = e.Id,
                                                      start = e.start,
                                                      end = e.end,
                                                      type = e.type
                                                     })
                                              .Concat( gEventBA.Select( e => new DataEvent ()
                                                                {
                                                                 eventId = e.Id,
                                                                 start = e.start,
                                                                 end = e.end,
                                                                 type = e.type
                                                                }))
                                              .OrderBy(e => e.eventId)),
                      //Project along keys
                      o => o,
                      anon => anon.Key,
                      (o,anon) => anon.EventGroup)
                      //Flatten groups
                           .SelectMany( g => g);
于 2012-11-18T22:27:45.273 回答