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, Func
1 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.ExpressionVisitor
1.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
155●23●23●28