我有一个事件ID列表,我想从我的select语句中排除,但不知道如何实现这个:
这是存储我的事件ID列表的原因
List<int> ExcludedEvents;
这是我的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
};
我想选择除eventEd匹配 EventShowCode 值
的事件以外的所有事件我查看了除运算符,但不知道如何实现它
答案 0 :(得分:4)
根据你问题中的代码,看起来应该是这样......
var filteredEvents = allEvents.Where(myEvent => !ExcludedEvents.Contains(myEvent.EventShowCode));
或者,如果您只想将其添加到select语句的末尾,只需将其放在Where中,然后将其从您之前的查询中选择结束时直接删除...
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));