English 中文(简体)
C# LINQ从名单中挑选
原标题:C# LINQ select from list
  • 时间:2010-07-30 10:19:14
  •  标签:
  • c#
  • linq

i 几周前,我曾问过这个问题,但愿得到任何建议的答复。

i 有一个从xml文件返回的活动清单,如下所示:

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 现在需要查询我的数据库,以配合与活动同等的活动。 从上述方法返回。 因此,我有这样的想法:

* E/CN.6/2009/1。 Id in GetEventIdsByEventDate

如何利用LINQ这样做

thanks kb

根据你的建议,衍生方法如下:

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

但是,在汇编中,我有以下错误:

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.

G. 清洁 数据库:

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);
    }
最佳回答

www.un.org/Depts/DGACM/index_french.htm 插图中数字,包含活动Ids(如方法名称所示):

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

另外,不要忘记将<代码>foo()方法改名为更合适的名称(例如)。 GetEventsByEventDate()

问题回答

您的错误:

G. 清洁 通过将含有异构造物体的电子识别器退回,因此,如果你使用“集装箱”方法,它期望“EventFeed”物体与清单中的物体相比较。 反之,你顺便说一遍,我假定这是一种愤怒或 something:

EventShowCode = eventsList.EventId

我认为,你回顾的是:

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

    return foo;
}    

页: 1 它免费,但经过升级的版本提供了英特尔利斯群岛的支持。 这一数字有助于我发现一些极其复杂的准则问题。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签