6

我有一个要从我的选择语句中排除的事件 ID 列表,但不知道如何实现:

这是存储我的事件 ID 列表的内容

List<int> ExcludedEvents;

这是我的选择语句(来自 XML 提要)

var allEvents = from eventsList in xmlDoc.Elements("shows").Elements("Show")
                select new EventFeed()
                  {
                      EventName = eventsList.Attribute("Name").Value,
                      EventSummary = eventsList.Attribute("ShortDesc").Value,
                      EventDetails = eventsList.Attribute("LongDesc").Value,
                      EventShowCode = eventsList.Attribute("Code").Value
                  };

我想选择除 eventId 与EventShowCode值匹配的事件之外的所有事件

我看过except运算符,但不知道如何实现它

4

1 回答 1

4

根据您在问题中的代码,它应该看起来像这样......

var filteredEvents = allEvents.Where(myEvent => !ExcludedEvents.Contains(myEvent.EventShowCode));

或者,如果您只是想将其添加到您的 select 语句的末尾,只需将其放在 Where 并将其放在您之前查询的 Select 的末尾...

var filteredEvents = xmlDoc.Elements("shows").Elements("Show") 
                     .Select( new  
                     { 
                         EventName = eventsList.Attribute("Name").Value, 
                         EventSummary = eventsList.Attribute("ShortDesc").Value, 
                         EventDetails = eventsList.Attribute("LongDesc").Value, 
                         EventShowCode = eventsList.Attribute("Code").Value 
                     })
                     .Where(myEvent => !ExcludedEvents.Contains(myEvent.EventShowCode));
于 2010-04-12T14:14:59.710 回答