开发者

Generic List .First not working LINQ

        var stuff = ctx.spReport();
        va开发者_StackOverflow社区r StuffAssembled = new List<ReportCLS>();
        var val = new List<ReportCLS>();
        foreach (var item in stuff)
        {
            StuffAssembled.Add(new ReportCLS(item));

        }

        val.Add(StuffAssembled.First());

Keeps throwing

System.Collections.Generic.List' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

what is going wrong ?

moreover how do i fix it?

Thanks


you should add this to your using statements:

using System.Linq;


A few things to check:

  • You're targeting .NET 3.5 or higher (or you're using LINQBridge)
  • You have a reference to the System.Core assembly
  • You have a using directive for System.Linq

Basically what the error message suggests...

EDIT: Additionally, your current code could be a lot simpler:

 var stuff = ctx.spReport();
 var stuffAssembled = stuff.Select(x => new ReportCLS(x)).ToList(); 
 var val = new List<ReportCLS> { stuffAssembled.First() };

Also, if you're actually using a List<T> then you might as well just use list[0] instead of list.First() :) Both will throw an exception if the list is empty, although the exception will differ, of course.


This compile-time error usually occurs when you

  • forgot to include imports for LINQ extensions (using System.Linq)
  • forgot to reference assembly with LINQ extensions
  • targeting 2.0 framework, which does not include LINQ by default
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜