Does the non-static method require a goal?

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; // More code } 

Alas, I still get the error: Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault();

+8
linq
source share
3 answers

It happened to me today. Come to find out I did this:

 Player player = db.Players .Where(p => p.ClubID == course.Club.ID && p.IsActive == true && p.Phone != null) .ToArray() .SingleOrDefault(p => p.Phone.FormatPhoneNumber() == phone); 

where course.Club was loading via EF through my database. At first I thought the problem was with the FormatPhoneNumber extension, but then I found that deleting course.Club.ID fixes the problem:

 int clubID = course.Club.ID; Player player = db.Players .Where(p => p.ClubID == clubID && p.IsActive == true && p.Phone != null) .ToArray() .SingleOrDefault(p => p.Phone.FormatPhoneNumber() == phone); 

Thus, avoid using values ​​derived from lazy objects in subsequent LINQ queries - assign them to local variables, and then use these variables in your query.

+18
source share

It turns out, because the name and type can be a null type, must be indicated on the side of the if statement, and therefore I have to check if the type and name are null before continuing:

 name = collumns[40]; type = db.Locations.Where(l => l.name == name).FirstOrDefault(); if (name != null && type != null) { Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault(); //More code.... } 
+4
source share

It is a fact that the problem is with the type object. I would decide like this:

 var tType = type?.type; Location loc = db.Locations.Where(l => l.name == name && (type != null && l.type == tType)).FirstOrDefault(); 
0
source share

All Articles