The*_*ebs -4 c# entity-framework asp.net-mvc-3
我在ASP中遇到的最令人困惑的错误.我之前已经完成了这样的方法调用,并且在我的代码的其他位置没有问题.
首先是班级:
namespace LocApp.Helpers.Classes.LocationHelper
{
public class QueryHelper
{
private LocAppContext db = new LocAppContext();
public static IEnumerable<Service> getAllService()
{
using (var db = new LocAppContext())
{
var service = db.Locations.Include(s => s.LocationAssignment);
var serv = (from s in db.Services
where s.active == true
select s).ToList();
return serv;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
很容易理解发生了什么.所以我们调用方法:
IEnumerable<LocApp.Models.Service> Service = new LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(Model.id);
Run Code Online (Sandbox Code Playgroud)
getAllServices(Model.id) 抛出错误"是一种方法,但被视为一种类型",嗯,不,它不被视为一种类型....
这是怎么回事?
好吧,正如错误信息所说的那样.getAllService()是一种方法:
public static IEnumerable<Service> getAllService()
Run Code Online (Sandbox Code Playgroud)
但是你试图使用它,好像它是一个带有构造函数的类型:
Service = new LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(...)
Run Code Online (Sandbox Code Playgroud)
这new部分是错误的.你不想调用构造函数,你只想调用一个方法.这是一个静态方法,因此您不需要实例 - 您可以使用:
Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(...)
Run Code Online (Sandbox Code Playgroud)
请注意,如果您有适当的using指令,请遵循.NET命名约定并注意单数/复数名称,您的代码将更容易理解:
var services = QueryHelper.GetAllServices(...);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |