-4

The most confusing error I have ever seen in ASP. I have done method calls like this before, and have no issue in other spots of my code.

First of all the class:

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

Pretty easy to understand whats going on. So lets call the method:

IEnumerable<LocApp.Models.Service> Service = new LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(Model.id);

getAllServices(Model.id) is throwing the error "is a method but treated like a type" , um no its not be treated like a type....

whats going on?

4

3 回答 3

5

好吧,就像错误消息所说的那样。getAllService()是一种方法:

public static IEnumerable<Service> getAllService()

但是你试图使用它,就好像它是一个带有构造函数的类型:

Service = new LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(...)

new部分是这里的错误。你不想调用构造函数,你只想调用一个方法。这是一个静态方法,所以你不需要实例 - 你可以使用:

Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService(...)

请注意,如果您有适当的using指令,遵循 .NET 命名约定并注意单数/复数名称,您的代码将更容易遵循:

var services = QueryHelper.GetAllServices(...);
于 2013-04-19T15:54:30.593 回答
2

你不是简单的意思吗:

IEnumerable<LocApp.Models.Service> Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService();

从本质上讲,摆脱该new位,并且该方法也不使用任何参数-我假设您在删除该new位后会遇到该问题。

于 2013-04-19T15:54:41.760 回答
1

您的 getAllService 方法不带任何参数,因此您应该在没有参数的情况下调用它。它也是一个静态方法,所以不要使用new关键字:

IEnumerable<LocApp.Models.Service> Service = LocApp.Helpers.Classes.LocationHelper.QueryHelper.getAllService();
于 2013-04-19T15:54:44.767 回答