I have some classes:


public class ProductInstance : Entity
{
        [Field, Key]
        public int Id { get; set; }

    [Field]
    public Product Product { get; set; }

            [Field]
    public bool IsWaybilled { get; set; }

    [Field]
    public bool IsChecked { get; set; }

public static Func<ProductInstance, bool> IsAvialableForSale() { return pi => !pi.IsChecked && !pi.IsWaybilled; } }

public class Product : Entity { [Field, Key] public int Id { get; set; }

    [Field]
    public ProductModel ProductModel { get; set; }

           [Field]
    [Association(PairTo = "Product", OnOwnerRemove = OnRemoveAction.Deny, OnTargetRemove = OnRemoveAction.Clear)]
    public EntitySet<ProductInstance> InstanceSet { get; set; }

}

public class ProductModel : Entity { [Field, Key] public int Id { get; set; }

    [Field]
    public string Name { get; set; }

    [Field]
    [Association(PairTo = "ProductModel", OnOwnerRemove = OnRemoveAction.Deny, OnTargetRemove = OnRemoveAction.Clear)]
    public EntitySet<Product> Products { get; set; }

}

And some code to work with entities:


var model = Session.Demand().Query.All<productmodel>().FirstOrDefault();
//works
var availProductInstanceCount = Session.Demand().Query.All<productinstance>()
                .Where(pi => pi.Product.ProductModel == model)
                .Where(ProductInstance.IsAvialableForSale())
                .Count();

//Works var test1 = model.Products.SelectMany(p => p.InstanceSet.Where(pi => !pi.IsChecked && !pi.IsWaybilled)).Count();

//don't works var test2 = model.Products.SelectMany(p => p.InstanceSet.Where(ProductInstance.IsAvialableForSale())).Count();

I got exception:


  Message=Unable to translate 'Query.All().Where(p => (p.ProductModel.Key == $@<Owner>({ Owner = ProductModel, (5) }).Owner.Key)).SelectMany(p => p.InstanceSet.Where(ProductInstance.IsAvialableForSale())).Count()' expression. See inner exception for details.
    StackTrace:
       at Xtensive.Orm.Linq.QueryProvider.TranslateTResult
       at Xtensive.Orm.Linq.QueryProvider.ExecuteTResult
       at System.Linq.Queryable.CountTSource
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation)
  InnerException: System.InvalidCastException
       Message=Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.LambdaExpression'.
       Source=Xtensive.Core
       StackTrace:
            at Xtensive.Core.ExpressionExtensions.StripQuotes(Expression expression)
            at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
            at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
            at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
            at Xtensive.Linq.ExpressionVisitor1.Visit(Expression e)
            at Xtensive.Orm.Linq.Translator.Visit(Expression e)
            at Xtensive.Orm.Linq.Translator.VisitSelectMany(Expression source, LambdaExpression collectionSelector, LambdaExpression resultSelector, Expression expressionPart)
            at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
            at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
            at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
            at Xtensive.Linq.ExpressionVisitor1.Visit(Expression e)
            at Xtensive.Orm.Linq.Translator.Visit(Expression e)
            at Xtensive.Orm.Linq.Translator.VisitSequence(Expression sequenceExpression, Expression expressionPart)
            at Xtensive.Orm.Linq.Translator.VisitSequence(Expression sequenceExpression)
            at Xtensive.Orm.Linq.Translator.VisitAggregate(Expression source, MethodInfo method, LambdaExpression argument, Boolean isRoot, MethodCallExpression expressionPart)
            at Xtensive.Orm.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind)
            at Xtensive.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression mc)
            at Xtensive.Orm.Linq.Translator.VisitMethodCall(MethodCallExpression mc)
            at Xtensive.Linq.ExpressionVisitor`1.Visit(Expression e)
            at Xtensive.Orm.Linq.Translator.Visit(Expression e)
            at Xtensive.Orm.Linq.Translator.TranslateTResult
            at Xtensive.Orm.Linq.QueryProvider.TranslateTResult
       InnerException: 

asked Jun 20 '11 at 13:50

Ness's gravatar image

Ness
155232328

edited Jun 20 '11 at 14:03


One Answer:

For use in queries you should use Expression<func<productinstance, bool="">> instead of Func<productinstance, bool="">

answered Jun 21 '11 at 03:17

pil0t's gravatar image

pil0t
207575763

pil0t is right; overwise LINQ translator simply can't investigate the contents of the func.

(Jun 21 '11 at 03:55) Dmitri Maximov Dmitri%20Maximov's gravatar image

I replaced code like this:


public static Expression<func<productinstance, bool="">> IsAvialableForSale()
        {
            Expression<func<productinstance, bool="">> ex =    pi => !pi.Store.IsPrimary && !pi.IsChecked && !pi.IsWaybilled && !pi.IsTemporary && !pi.IsReserved;

        return ex;
    }

And I still get the same error.

(Jun 21 '11 at 04:06) Ness Ness's gravatar image

I see. Will check.

(Jun 21 '11 at 04:10) Dmitri Maximov Dmitri%20Maximov's gravatar image

The bug is confirmed. Thank you for the sample model, it helped a lot.

(Jun 21 '11 at 04:57) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA