Reputation: 222
LINQ: Typing my first ever LINQ query and the system is not recognizing my code line which is as follows:
int[] i= { 1, 2, 3, 4, 5 };
IEnumerable<int> j = from r in i select r;
My "i" bears red squiggly saying - a field initializer cannot reference the nonstatic field method or property
Upvotes: 0
Views: 45
Reputation: 149518
A field initializer cannot reference the nonstatic field method or property
This error means you're trying to initialize your IEnumerable<T>
inside a class
level declaration with a LINQ query. If you want to initialize that field, do so inside the class constructor:
public class SomeClass
{
int[] I = { 1, 2, 3, 4, 5 };
IEnumerable<int> J { get; set; }
public SomeClass()
{
J = from r in i select r;
}
}
Upvotes: 1