非静态方法需要目标吗? [英] Non-Static method requires a target?

查看:73
本文介绍了非静态方法需要目标吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未见过此错误,而且非常混乱,我基本上是想做些什么,使我说找到与传入的位置名称和类型相匹配的所有位置(只会返回一个):

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

也许有一种更好的方法可以一口气完成我想做的事情,但是从本质上来说,我是从一列中获取名称的(这来自一个csv文件),然后说,让我知道位置信息.在此之后,我已经说完了爵士乐,现在就说吧,让我找到一个具有此名称及其类型的位置.

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.

但是我得到了错误:

非静态方法需要一个目标

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 是上下文类,这很明显.
  • 是我从csv文件中提取数据,在这种情况下,列[40]类似于纽约"
  • 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
}

A,我仍然在以下位置收到错误:Location loc = db.Locations.Where(l => l.name == name && l.type == type.type).FirstOrDefault();

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

推荐答案

我今天遇到了这种情况.来找出答案,我正在这样做:

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

其中course.Club是通过EF从我的数据库中延迟加载的.起初,我以为我的问题是FormatPhoneNumber扩展名,但是后来发现删除course.Club.ID可以解决此问题:

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

因此,避免在后续的LINQ查询中使用从延迟加载的对象中收集的值-将它们分配给局部变量,然后在查询中使用这些变量.

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.

这篇关于非静态方法需要目标吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆