I have never seen this error before and it is very confusing, I, in fact, try to do something where I say, find all the locations (only return one) that match the name of the place that was transferred and the type:
string name = columns[40]; Location type = db.Locations.Where(l => l.name == name).FirstOrDefault(); Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault();
Probably the best way to do what I want in one fell swoop, but essentially I get the name from the column (this comes from the csv file) and then say to get information about such places. After that I say “OK” that I have all this jazz, go get me a place with this name and its type.
But I get the error:
The non-static method requires a targeted
The top-level method in which all this code is executed:
static void Main(string[] args){}
In fact, this is just a console application. So what's going on?
- db is a context class, this should be obvious.
- columns - this is to extract data from the csv file, in this case the columns [40] will look like "New York".
Full error message from the stack trace: {"A non-static method requires a target."}
Note. A question posted as a “possible answer” does not help in this case, since the main method that I run this code is static.
After further research, I found that the name and type were empty, so I made the following correction:
if (name != null) { Location type = db.Locations.Where(l => l.name == name).FirstOrDefault(); Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault(); locationNearbyId = loc.id;
Alas, I still get the error: Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault();
linq
Thewebs
source share