linq查询中的异常,使用“Type”作为属性名称

时间:2012-06-20 04:46:38

标签: c# linq nhibernate casting linq-to-nhibernate

我有一个linq对nhibernate的查询。

var q = SessionInstance.Query<Evidence>()
       .Select(x => new Evidence(x.Id)
        {
            Type = x.Type,
            StartDate = x.StartDate,
            EndDate = x.EndDate
        })    
        .ToList();

此消息有例外:

NHibernate.Exceptions.GenericADOException : Could not execute query[SQL: SQL not available] {"Exception has been thrown by the target of an invocation."} {"Specified cast is not valid."}

但是,通过将Type属性重命名为Type1,我的问题已得到解决。

q = q.Select(x => new Evidence(x.Id)
    {
        Type1 = x.Type1,
        StartDate = x.StartDate,
        EndDate = x.EndDate
    });

为什么呢?

是否存在使用Type名称作为属性的解决方案?

更新 证据类是:

public class Evidence
{
    public long Id;
    public EvidenceEnumType? Type;
    public DateTime? StartDate;
    public DateTime? EndDate;
}

堆栈跟踪

  at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
   at NHibernate.Impl.ExpressionQueryImpl.List()
   at NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
   at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at RCISP.NHibernate.Repository.EvidenceIssuanceRepositoryNh.Search(SearchEvidenceIssuanceDto dto)
   at RCISP.Domain.Services.EvidenceIssuanceService.SearchCommand(IRepositoryLocator locator, SearchEvidenceIssuanceDto dto) in D:\Users\Zeynali\Aseman\src\RCISP.Domain\Services\Issuance\EvidenceIssuanceService.cs:line 62
   at RCISP.Domain.Services.EvidenceIssuanceService.<>c__DisplayClass4.<Search>b__3(IRepositoryLocator locator) in D:\Users\Zeynali\Aseman\src\RCISP.Domain\Services\Issuance\EvidenceIssuanceService.cs:line 58
   at RCISP.NHibernate.TransManager.TransManagerNh.ExecuteCommand[TResult](Func`2 command)

1 个答案:

答案 0 :(得分:0)

我相信您可以使用@符号让编译器知道您正在键入属性名称:

var q = SessionInstance.Query<Evidence>()
       .Select(x => new Evidence(x.Id)
        {
            Type = x.@Type,
            StartDate = x.StartDate,
            EndDate = x.EndDate
        })    
        .ToList();

编辑: 嗯,我刚尝试制作一个模型情况:

public class MyClass
{
    public string Type;
}

var y = new List<MyClass>().Select(x => new MyClass() {Type = x.Type});

它似乎正在编译所以我现在看到这不是一个编译时问题(你的问题本来应该是显而易见的)但可能是一个反思问题......所以让我知道@符号实际上是否修复了......