LINQ“例外”操作员

发布于 2024-08-28 08:55:02 字数 775 浏览 6 评论 0原文

我有一个事件 ID 列表,我想将其从 select 语句中排除,但不确定如何实现:

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

List<int> ExcludedEvents;

,这是我的 select 语句(来自 XML feed),

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 运算符,但不确定如何实现它

i have a list of event Ids that i want to be excluded from my select statement, but no sure how to implement this:

this is what stores my list of event Ids

List<int> ExcludedEvents;

and this is my select statement (from an XML feed)

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
                  };

i want to select all events except for the events that have their eventId matching the EventShowCode value

i have looked at the except operator, but not sure how to implement it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-09-04 08:55:02

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

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

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

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));

based on your code in the question, it should look something like this...

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

Or, if you just wanted to tack it on to the end of your select statement, just take that Where and drop it right at the end of your Select from your previous query...

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文