带有IQueryable的MVC3中的PagedList

时间:2012-08-09 08:59:37

标签: asp.net-mvc asp.net-mvc-3 pagedlist

我无法理解我做错了什么。每次我收到此错误:

无法在LINQ to Entities查询中构造实体或复杂类型“BusinessLogic.CompanyWithDivisionCount”。

我需要从“公司”表中获取信息,并从“部门”表中获取每个公司的部门数,然后制作PagedList。这是我的'公司'表:

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

using BusinessLogic.Services;
using BusinessLogic.Models.ValidationAttributes;

namespace BusinessLogic.Models
{
    public class Company
    {
        public Company()
        {
            Country = "US";
            Status = true;
        }

        public int Id { get; set; }

        [Required]
        [UniqueCompanyName]
        public string Name { get; set; }

        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public int Zip { get; set; }
        public string Country { get; set; }

        public string ContactInfo { get; set; }

        [Required]
        public DateTime EffectiveDate { get; set; }
        public DateTime TerminationDate { get; set; }

        public bool Status { get; set; }

        [Required]
        public string URL { get; set; }
        public string EAP { get; set; }

        public string EAPCredentials { get; set; }

        public string BrandingColors { get; set; }

        public string Comments { get; set; }
    }
}

这是我的域名模型:

public class Company
{
    public Company()
    {
        Country = "US";
        Status = true;
    }

    public int Id { get; set; }

    [Required]
    [UniqueCompanyName]
    public string Name { get; set; }

    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
    public string Country { get; set; }

    public string ContactInfo { get; set; }

    [Required]
    public DateTime EffectiveDate { get; set; }
    public DateTime TerminationDate { get; set; }

    public bool Status { get; set; }

    [Required]
    public string URL { get; set; }
    public string EAP { get; set; }

    public string EAPCredentials { get; set; }

    public string BrandingColors { get; set; }

    public string Comments { get; set; }
}

public class CompanyWithDivisionCount: Company // I'm using this
{
    public int DivisionCount { get; set; }
}

这是我的控制器:

public ActionResult CompaniesList(int? page)
{
    var pageNumber = page ?? 1;

    var companies = companyService.GetCompaniesWithDivisionsCount2();

    var model = companies.ToPagedList(pageNumber, PageSize);

    return View(model);
}

这是我的服务部分:

public IQueryable<CompanyWithDivisionCount> GetCompaniesWithDivisionsCount2()
{
    return (from c in dataContext.Companies.AsQueryable()
            select new CompanyWithDivisionCount
            {
                Id = c.Id,
                Name = c.Name,
                Status = c.Status,
                EffectiveDate = c.EffectiveDate,
                URL = c.URL,
                EAP = c.EAP,
                EAPCredentials = c.EAPCredentials,
                Comments = c.Comments,
                DivisionCount = (int)dataContext.Divisions.Where(b => b.CompanyName == c.Name).Count()
            });
}

}

感谢您的帮助!!!

2 个答案:

答案 0 :(得分:1)

PagedList的创建者。这与PagedList无关,而是一个实体框架问题(我不是实体框架的专家,所以无法帮助你)。要确认这是真的,请按以下行编写单元测试:

[Test]
public void ShouldNotThrowAnException()
{
    //arrange
    var companies = companyService.GetCompaniesWithDivisionsCount2();

    //act
    var result = companies.ToList();

    //assert
    //if this line is reached, we win! no exception on call to .ToList()
}

答案 1 :(得分:0)

如果可能,我会考虑更改您的数据模型,以便不使用名称字符串将公司与分部相关联,而是使用两个对象之间正确维护的外键关系(分区应包含CompanyID外键)。这有很多好处(包括性能和数据完整性),如果您需要对应用程序进行进一步更改(或者任何公司决定它可能会重新命名它的名称),几乎肯定会让您的生活更轻松。

如果您创建了正确的外键关系,那么您的域模型可能看起来像

public class Company
{
    ...

    public virtual ICollection<Division> Divisions{ get; set; }

    public int DivisionCount
    {
         get 
         {
              return this.Divisions.Count()
         }
    }

    ...
}