Most Likely Cause of the C# Error: "Non-static method requires a target"
I've encountered this error twice in the last two days, both times occurring while with a LINQ query and Entity Framework.
And, in both cases, the problem stemmed from using a null value within the LINQ. Ultimately, the problem was farther upstream and not with my LINQ query at all (though better use of a try-catch statement would've been smart). The problem was an object with a null value being passed into the method. Once I figured that out, it was fairly easy to step through the debugger and see where my object wasn't being set to a legitimate value.
So, if you get the error "Non-static method requires a target,", then verify that every value you're using within your LINQ statement contains a value, not a null.
See the following posts on the web for more information and resolution to this error.
And, in both cases, the problem stemmed from using a null value within the LINQ. Ultimately, the problem was farther upstream and not with my LINQ query at all (though better use of a try-catch statement would've been smart). The problem was an object with a null value being passed into the method. Once I figured that out, it was fairly easy to step through the debugger and see where my object wasn't being set to a legitimate value.
So, if you get the error "Non-static method requires a target,", then verify that every value you're using within your LINQ statement contains a value, not a null.
See the following posts on the web for more information and resolution to this error.
- Non-static method requires a target
http://stackoverflow.com/questions/13717355/non-static-method-requires-a-target - EF Non-static method requires a target
http://stackoverflow.com/questions/15498981/ef-non-static-method-requires-a-target - Linq: Non-static-methods-requires-a-target with select new {model}
http://stackoverflow.com/questions/16944034/linq-non-static-methods-requires-a-target-with-select-new-model
Comments
Post a Comment