C#LINQ从列表中选择 [英] C# LINQ select from list

查看:51
本文介绍了C#LINQ从列表中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从xml文档返回的事件ID的列表,如下所示

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();  
    }

我现在需要查询数据库以匹配与上述方法返回的eventIds相等的事件.所以我会有类似的东西:

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:

从eventsdb中选择*,其中getEventIdsByEventDate()中的eventId

select * from eventsdb where eventId in GetEventIdsByEventDate()

我如何使用LINQ做到这一点

how can i do this using LINQ

我似乎无法获得任何答案.

i cant seem to get any of the answers working.

这是一种从XML提要中查找eventId的方法

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);
    }

这是在我的数据库中存在的XML中查找任何匹配的eventId的方法

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:

错误9参数'2':无法从'string'转换为'Events.EventsDetails'

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

推荐答案

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

如果要在方法内部返回List<EventFeed>,则应将方法返回类型从IEnumerable<EventFeed>更改为List<EventFeed>.

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

这篇关于C#LINQ从列表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆