Linq将NHibernate投射到anon。类型导致错误的转换错误

时间:2010-09-03 10:07:56

标签: linq anonymous-types linq-to-nhibernate projection

我有一个使用NHibernate持久化的TaxWork实体。该实体具有以下属性(以及其他):

public virtual TaxWorkType Type { get; set; } //Kctc.TaxWorkType is an enumeration
public virtual TaxWorkStatus Status { get; set; } //Kctc.TaxWorkStatus is an enumeration
public virtual LegalWorkPriority Priority { get; set; } //Kctc.LegalWorkType is an enumeration
public virtual User Handler { get; set; } //Kctc.BusinessLayer.Entities.User is another entity
public virtual int? CaseNumber { get; set; }

我正在使用Linq到NHibernate来提取税务工作对象的子集,如下所示(taxWorkRepository.All显然会返回IQueryable):

foreach (TaxWork taxWork in taxWorkRepository.All.Where(x => x.CaseNumber == _caseNumber).OrderBy(x => x.DateCreated))
{
    ...
}

这很好用。我想使用投影,以便只查询在这种情况下所需的列。我是以下代码:

foreach (var taxWorkFragment in taxWorkRepository.All.Where(x => x.CaseNumber == _caseNumber).OrderBy(x => x.DateCreated).Select(x => new { Type = x.Type, DateCreated = x.DateCreated, Handler = x.Handler, Status = x.Status, Priority = x.Priority }))
{
    ...
}

但是,在尝试创建匿名类型时出现以下错误:

从'Kctc.TaxWorkStatus'到'Kctc.BusinessLayer.Entities.User'的无效演员。

它究竟应该将TaxWorkStatus转换为用户?

任何可能出错的建议?

1 个答案:

答案 0 :(得分:0)

尝试这样做:

foreach (var taxWorkFragment in taxWorkRepository.All.Where(x => x.CaseNumber == _caseNumber).OrderBy(x => x.DateCreated)
.Select(x => new TaxWork { Type = x.Type, DateCreated = x.DateCreated, Handler = x.Handler, Status = x.Status, Priority = x.Priority }))
{
...
}

应该有帮助