C# LINQ select from list

旧城冷巷雨未停 提交于 2019-12-03 15:09:35

问题


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:


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




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/3117003/c-sharp-linq-select-from-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!