nhibernate查询选择特定的colmns,包括IList

时间:2014-02-10 19:55:57

标签: sql linq nhibernate select ienumerable

我在选择specyfic列(属性)时出现问题,代码如下:

实体:

public class Item {
        public Item() {
            Images = new List<Image>();
        }
        public virtual int Id { get; set; }
        public virtual int ItemNum { get; set; }
        public virtual Channel Channel { get; set; }
        public virtual string Title { get; set; }
        public virtual string Comment { get; set; }
        public virtual string Text { get; set; }
        public virtual string Link { get; set; }
        public virtual string Category { get; set; }
        public virtual DateTime? PublishDate { get; set; }
        public virtual string ExternalId { get; set; }
        public virtual string SourceImage { get; set; }
        public virtual string Bubble { get; set; }
        public virtual IEnumerable<Image> Images { get; set; }
    }
    public class Image {
    public Image() { }
    private string type;
    public virtual int Id { get; set; }
    public virtual bool IsMobile { get; set; }
    public virtual Item Item { get; set; }
    public virtual string Type {get;set;}
    public virtual string ImageUrl { get; set; }
    public virtual int? Height { get; set; }
    public virtual int? Width { get; set; }
    public virtual int? ImgXOff { get; set; }
    public virtual int? ImgYOff { get; set; }
    public virtual decimal? ImgZoom { get; set; }
}

现在我想只获得Item.Id和所有与此ID相关的行,我的查询

var minDate = DateTime.Today - TimeSpan.FromDays(config.ItemsFromLastDaysCount);
        var items = session.Query<Item>()
            .Fetch(x => x.Channel)
            .Where(x => x.Channel.InsertionDate >= minDate)
            .OrderByDescending(x => x.Channel.InsertionDate)
            .Take(config.ItemsOnOnePage)
            .Select(x => new 
            {
                Id = x.Id,
                Images = x.Images
            }).ToList();

但像这样的查询引起了我的异常:

“System.Core.dll中发生了'System.ArgumentException'类型的异常,但未在用户代码中处理

附加信息:'System.Collections.IList'类型的表达式不能用于'System.Collections.Generic.IEnumerable`1 [System.Object]''

类型的参数

堆栈追踪:

   at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
   at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)
   at System.Linq.Expressions.Expression.Invoke(Expression expression, IEnumerable`1 arguments)
   at System.Linq.Expressions.Expression.Invoke(Expression expression, Expression[] arguments)
   at NHibernate.Linq.ExpressionToHqlTranslationResults.MergeLambdasAndCompile(IList`1 transformations)
   at NHibernate.Linq.ExpressionToHqlTranslationResults..ctor(HqlTreeNode statement, IList`1 itemTransformers, IList`1 listTransformers, IList`1 postExecuteTransformers, List`1 additionalCriteria)
   at NHibernate.Linq.IntermediateHqlTree.GetTranslation()
   at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root)
   at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at TicTac.Service.Controllers.HomeController.Index() in d:\Projekty\Hypermedia\TicTac.RssReader\branches\TicTac.RssReader\TicTac.Service\Controllers\HomeController.cs:line 27
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()

异常是因为行:

Images = x.Images

我对回应非常满意。

0 个答案:

没有答案