6

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

bzlm
  • 9,626
  • 6
  • 65
  • 92
kb.
  • 1,010
  • 3
  • 17
  • 34
  • 2
    are you sure about Linq to SQL? is not it Linq to XML? – Andrey Apr 12 '10 at 14:12
  • @Andrey It's just LINQ. You can't extend the provider, only add filtering on top of it, so it's not "to" anything. :) – bzlm Apr 12 '10 at 14:23
  • @bzlm there is no just Linq. it's simplest form is Linq to Objects then. – Andrey Apr 12 '10 at 14:26
  • You don't want to use the Except extension here. Except is for when you are comparing collections of the same type. In this case, you are comparing an integer list with a collection of XML elements. Use Scott's suggestion. – Audie Apr 12 '10 at 14:36
  • @Audie, thanks for the tip, scotts answers works a treat! – kb. Apr 12 '10 at 14:38

1 Answers1

4

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));
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118