开发者

Generic List .First not working LINQ

开发者 https://www.devze.com 2022-12-28 12:36 出处:网络
var stuff = ctx.spReport(); va开发者_StackOverflow社区r StuffAssembled = new List<ReportCLS>();
        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

精彩评论

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