【问题标题】:Adding multiple parameters to constructor with MVC2使用 MVC2 向构造函数添加多个参数
【发布时间】:2011-06-30 11:21:27
【问题描述】:

我有以下 LINQ 查询,它从我的数据库中返回两个对象。这些对象将被强类型化到显示模板的ViewModel 使用:

 public IQueryable<ICustomerSiteRepository> CustomerAndSites
 {
     get
     {
         return from customer in customerTable
                    join site in customerSitesTable
                        on customer.Id equals site.CustomerId
                select new CustomersAndSitesMix(customer, site);
     }
}

我正在尝试创建一个新的CustomersAndSitesMix 类,其构造函数接受两个参数(客户和站点)。

但是,当我创建类并尝试像这样设置构造函数时:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomerDatabase.Domain.Entities
{
    public class CustomersAndSitesMix (CustomerSite custSite, Customer cust)
    {
    }
}

我收到语法错误,指出在 for、using 或 fixed 声明中不能使用多个类型。
我做错了什么?

【问题讨论】:

    标签: linq asp.net-mvc-2 repository viewmodel


    【解决方案1】:

    你应该先声明类:

    // This is the namespace
    namespace CustomerDatabase.Domain.Entities
    {
        // This is the class declration
        public class CustomersAndSitesMix
        {
            // this is the constructor
            public CustomersAndSitesMix(CustomerSite custSite, Customer cust)
            {
            }
        }
    }
    

    【讨论】:

    • 感谢大家的快速响应,我的小学生错误。
    • 谢谢,我需要在这个方法中为我的对象添加单独的属性吗?我不认为它们在我的实体中,最终我想创建一个包含两个对象的属性的列表?
    • @Liam,您不应该在构造函数中添加属性。属性被添加到类中。
    • 对不起,这就是我在类中的意思,但我已经在实体和视图模型中定义了我的对象属性,我是否还需要在此类中添加它们才能使 LINQ 查询正常工作?
    【解决方案2】:

    在类中实现构造函数。不在命名空间内

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 2018-03-21
      • 2018-12-06
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多