1

I have a query by linq to nhibernate.

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

It has a exception by this Message :

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."}

But by rename Type property to Type1, My problem is resolved.

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

Why?

Is a solution exist for use Type name for a property?

UPDATE: Evidence class is :

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

Stack Trace

  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)
4

1 回答 1

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});

它似乎正在编译,所以我现在看到这不是编译时问题(你的问题应该很明显),但可能是反射问题......所以让我知道@符号是否真的修复了它...... .

于 2012-06-20T05:43:53.177 回答