【问题标题】:PagedList in MVC3 with IQueryable带有 IQueryable 的 MVC3 中的 PagedList
【发布时间】:2023-03-26 09:35:01
【问题描述】:

我不明白我做错了什么。每次我收到此错误时:

无法在 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()
            });
}

}

谢谢你的帮助!!!

【问题讨论】:

  • compile-time errorruntime error ?

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


【解决方案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()
}

【讨论】:

    【解决方案2】:

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

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

    public class Company
    {
        ...
    
        public virtual ICollection<Division> Divisions{ get; set; }
    
        public int DivisionCount
        {
             get 
             {
                  return this.Divisions.Count()
             }
        }
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2012-07-16
      • 1970-01-01
      相关资源
      最近更新 更多