Non-static method requires a target
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....
}
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.