Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ select from list

Tags:

i have a list of event Ids returned from an xml document as shown below

public IEnumerable<EventFeed> GetEventIdsByEventDate(DateTime eventDate)     {          return (from feed in xmlDoc.Descendants("Show")                 from ev in feed.Elements("Event")                 where Convert.ToDateTime(ev.Attribute("Date").Value).ToShortDateString() == eventDate.ToShortDateString()                 select new EventFeed()                 {                     EventShowCode = feed.Attribute("Code").Value                 }).ToList();       } 

i now need to query my database to match events that equal the eventIds returned from the above method. so i would have something like:

select * from eventsdb where eventId in GetEventIdsByEventDate()

how can i do this using LINQ


i cant seem to get any of the answers working.

this is the method that looks up the eventIds from an XML feed

public IList<EventsDetails> GetEventIds(DateTime eventDate)     {          var eventids = (from feed in xmlDoc.Descendants("Show")                         from ev in feed.Elements("Event")                         where Convert.ToDateTime(ev.Attribute("Date").Value).ToShortDateString() == eventDate.ToShortDateString()                         select new EventsDetails()                         {                             EventId = feed.Attribute("Code").Value                         }).ToList();          return eventids;     } 

this is the method that looks up the events in my database

public IEnumerable<EventFeed> GetAllEventsFromDatabase()     {         var allEvents = from eventsList in GetEventsList()                         select new EventFeed()                         {                             EventName = eventsList.Title,                             EventSummary = eventsList.Introduction,                             EventShowCode = eventsList.EventId,                             EventImageSmall = eventsList.EventImageThumbUrl,                             EventUrl = eventsList.Url,                             EventSortBy = eventsList.SortOrder                         };          return allEvents.OrderBy(x => x.EventSortBy);     } 

and this is the method to look up any matching eventIds in the XML that exist in my database

public IEnumerable<EventFeed> FilteredEvents(DateTime eventDate)     {          return GetAllEventsFromDatabase().Where(p => GetEventIds(eventDate).Contains<EventsDetails>(p.EventShowCode));      } 

the project fails to build with the following error:

Error 9 Argument '2': cannot convert from 'string' to 'Events.EventsDetails'

like image 963
kb. Avatar asked Jun 25 '10 09:06

kb.


1 Answers

        var eventids = GetEventIdsByEventDate(DateTime.Now);         var result = eventsdb.Where(e => eventids.Contains(e)); 

If you are returnning List<EventFeed> inside the method, you should change the method return type from IEnumerable<EventFeed> to List<EventFeed>.

like image 95
Thurein Avatar answered Sep 18 '22 15:09

Thurein