0

我有一个使用 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 to 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 投射给用户的想法?

有什么建议可能会出错吗?

4

1 回答 1

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 }))
{
...
}

它应该有帮助

于 2011-07-15T07:45:55.267 回答