24

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'

1
  • kb : Do you have a definitive answer for this yet? Could you please mark it so that we know the right answer to this. Commented Jun 25, 2010 at 12:31

4 Answers 4

30
        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>.

Sign up to request clarification or add additional context in comments.

Comments

14

In likeness of how I found this question using Google, I wanted to take it one step further. Lets say I have a string[] states and a db Entity of StateCounties and I just want the states from the list returned and not all of the StateCounties.

I would write:

db.StateCounties.Where(x => states.Any(s => x.State.Equals(s))).ToList();

I found this within the sample of CheckBoxList for nu-get.

Comments

5

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.