Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-Static method requires a target?

Tags:

linq

I have never seen this error before and its very confusing, I am essentially trying to do something where I say find me all locations (will only return one) that match the location name passed in 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();

There's probably a better way to do what I want in one fell swoop, but essentially I get the name from a column (this comes from a csv file), and then say, get me that locations information. After this I say ok now that I have all that jazz, go get me a location with this name and its type.

But I get the error:

Non-Static method requires a target

The top level method all this code runs in is:

static void Main(string[] args){}

Essentially its just a console app. So whats going on?

  • db is the context class, this should be obvious.
  • columns is me pulling the data from the csv file, in this case columns[40] would be something like "New York"

Full Error message from the stack trace: {"Non-static method requires a target."}

Note: The question posted as a "possible answer" does not help in this case as the main method I am running this code in is static.

Upon further investigation I found the name and type were null so I did the following fix:

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 at: Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault();

like image 868
TheWebs Avatar asked May 15 '13 17:05

TheWebs


2 Answers

I had this happen to me today. Come to find out, I was doing 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 being lazy-loaded via EF from my database. At first, I thought my problem was the FormatPhoneNumber extension, but then found that removing the course.Club.ID fixed the issue:

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);

So, avoid using values gleaned from lazy-loaded objects in subsequent LINQ queries - assign them to local variables and then use those variables in your query.

like image 181
Derreck Dean Avatar answered Nov 10 '22 12:11

Derreck Dean


Turns out that because name and type can be null type has to be set out side the if statement and thus i must check if type and name are null before continueing:

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....
}
like image 37
TheWebs Avatar answered Nov 10 '22 13:11

TheWebs