2012-08-09 40 views
1

我不明白我在做什麼錯。每當我得到這個錯誤:PagedList在MVC3與IQueryable的

實體或複雜類型「BusinessLogic.CompanyWithDivisionCount」不能在LINQ到實體查詢構造。

我需要從「公司」各公司從「司」表的表和部門計數的信息,然後進行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() 
      }); 
} 

}

感謝您的幫助!

+0

'編譯時錯誤'或'運行時錯誤'? – Yasser 2012-08-09 09:02:57

+0

只有運行時錯誤 – user1545035 2012-08-09 09:41:24

回答

0

我會考慮是否可能改變你的數據模型,使而不是按名稱串與公司分部,而不是使用兩個對象之間的正確維護外鍵關係(部門應該包含CompanyID外鍵)。這有很多好處(包括性能和數據完整性),如果您需要對應用程序進行進一步更改(或者任何公司決定重新命名它的名稱),幾乎肯定會讓您的工作更輕鬆地向前邁進。

如果你創建一個正確的外鍵關係,然後你的域模型可能看起來像

public class Company 
{ 
    ... 

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

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

    ... 
} 
1

造物主PagedList這裏。這與PagedList無關,而是一個實體框架問題(我不是Entity Framework的專家,所以不能幫助你)。要確認這是真的,請按照以下幾行編寫單元測試:

[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() 
}