tags:

views:

79

answers:

3

Hi

i asked this a few weeks ago, but couldnt get any of the suggested answers working, so i would be grateful for any help on this:

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

thanks kb

Hi Prutswonder, ive created the method below based on your suggestion

public IEnumerable<EventFeed> foo(DateTime str)
    {
        var foo = from f in GetAllEventsFromDatabase().ToList()
                  where GetAllEventsByDate(str).Contains(f.EventShowCode)
                  select e;

        return (IEnumerable<EventFeed>) foo;
    }

but on compile i get the following error

Error   7   The type arguments for method 'System.Linq.Enumerable.Contains<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

GetAllEventsFromDatabase:

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);
    }
+2  A: 

The GetEventIdsByEventDate() method should return an IEnumerable of strings, containing the Event Ids (like the method name implies):

public IEnumerable<string> 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 feed.Attribute("Code").Value 
            ).ToList();   
} 

Also, don't forget to rename the foo() method to a more suitable name (for example GetEventsByEventDate())

Prutswonder
Hi Prutswonder, i have updated my post in response to your solution, please could you have a look at this? thansk kb
kb
Updated my answer based on your post. ;-)
Prutswonder
A: 

Download LINQPad. It's free but the upgraded version provides Intellisense support. This app has helped me figure out some pretty complicated LINQ queries.

Keith
FYI, this shouldn't really be an "answer". It should be a comment.
Ocelot20
+1  A: 

About your error:

GetAllEventsByDate returns an IEnumerable containing EventFeed objects, so when you use the "Contains" method, it expects an "EventFeed" object to compare to the objects in the list. Instead, you are passing it an f.EventShowCode, which I assume is an integer or something:

EventShowCode = eventsList.EventId

I believe what you're looking for is this:

public IEnumerable<EventFeed> foo(DateTime str)    
{    
    var foo = from f in GetAllEventsFromDatabase()
              where GetAllEventsByDate(str).Contains(f)    
              select f;

    return foo;
}    
Ocelot20
thanks BPotocki, your answer was also helpful in solving this
kb