tags:

views:

86

answers:

3

Hi

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'

A: 

Execute the GetEventIdsByEventDate() method and save the results in a variable, and then you can use the .Contains() method

Vedran
+1  A: 

The "in" in Linq-To-Sql uses a reverse logic compared to a SQL query.

Let's say you have a list of integers, and want to find the items that match those integers.

int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

var items = from p in context.Items
                 where numbers.Contains(p.ItemId)
                select p;

Anyway, the above works fine in linq-to-sql but not in EF 1.0. Haven't tried it in EF 4.0

Matteo Mosca
A: 
        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>.

Kthurein