The nhibernate request selects specific conflicts, including IList

I had a problem with choosing special columns (properties), the code below:

Entities:

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

And now I would like to get only Item.Id and everything related to the string with this Id, my request

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

But such a request raises a strange exception:

"An exception of type" System.ArgumentException "occurred in System.Core.dll, but was not processed in user code

Additional information: an expression of type 'System.Collections.IList' cannot be used for a parameter of type 'System.Collections.Generic.IEnumerable`1 [System.Object]' "


Stack trace:

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

the exception is due to the line:

Images = x.Images

I will be very grateful for the answer.

+4

Source: https://habr.com/ru/post/1526233/


All Articles