LINQ"除了"运算符

kb.*_*kb. 6 linq except

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

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

List<int> ExcludedEvents;
Run Code Online (Sandbox Code Playgroud)

这是我的select语句(来自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
                  };
Run Code Online (Sandbox Code Playgroud)

我想选择所有事件,除了eventId匹配EventShowCode值的事件

我查看了except运算符,但不确定如何实现它

Sco*_*vey 4

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

var filteredEvents = allEvents.Where(myEvent => !ExcludedEvents.Contains(myEvent.EventShowCode));
Run Code Online (Sandbox Code Playgroud)

或者,如果您只是想将其附加到 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));
Run Code Online (Sandbox Code Playgroud)