I want to define a Func<ProductItemVendor, bool>
filter expression named CompareProductItemVendorIds
, which can be used throughout my application, mostly within Entity Framework/LINQ queries.
I've learned that in order to be able to use this filter in a LINQ query, I must declare it as Expression<Func<>>
instead of just Func<>
. I understand the reason for this, and it's easy for me to do this.
But I'm having the following problems using that expression in my queries.
First, code such as:
ProductItem.ProductItemVendors.FirstOrDefault(CompareProductItemVendorIds)
Note: ProductItem
is a database entity, and its ProductItemVendors
property is a navigation collection.
Produces the error:
`Instance argument: cannot convert from 'System.Collections.Generic.ICollection' to 'System.Linq.IQueryable'
And second, code such as:
var results = from v in Repository.Query<ProductItemVendor>()
where CompareProductItemVendorIds(v)
select v;
Produces the error:
'CompareProductItemVendorIds' is a 'variable' but is used like a 'method'
So I have my nice shiny new Expression<Func<>>
. How can I use it in my LINQ queries?
Func<>
directly? – EraserThe LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
Please see https://mcmap.net/q/1469586/-using-func-lt-gt-in-linq-query. – GeniusFunc<ProductItemVendor, bool> CompareProductItemVendorIds = new Func<ProductItemVendor, bool>(r => r.ID > 10); var x = ProductItemVendors.FirstOrDefault(CompareProductItemVendorIds);
– RockiesProductItem
is a database entity, and it'sProductItemVendors
property is a navigation collection. – GeniusCompareProductItemVendorIds
is now anExpression
? Then you should share the code that build it. – ScalawagExpression<T>
. – ScalawagExpression<Func<>>
". – CurveFunc<>
worked withWhere()
but not in a query. The answer was that the most performant way to get it to work was to make it anExpression<Func<>>
. So now I have anExpression<Func<>>
but I can't use it in my query. (I think it will work fine inWhere()
, but that's not the type of query I'm using.) I've reviewed the helpful answers in the other question, but do not see where they address this. – GeniusExpression<Func<ProductItemVendor, bool>>
? – CurveProductItem.ProductItemVendors.FirstOrDefault(CompareProductItemVendorIds)
is just an expression, could you give the entire statement? (context matter ALOT when it comes to EF) – CarriecarrierProductItemVendor productItemVendor = ProductItem.ProductItemVendors.FirstOrDefault(CompareProductItemVendorIds);
, but I don't see how that can be helpful. – GeniusExpression
". There's half a page of text there. – RomaromagnaExpression<Func<>>
instead of justFunc<>
. So now I have anExpression<Func<>>
but couldn't see a way to incorporate it into the type of queries I'm using. I've reviewed the previous question and don't see an answer to that. Just saying I missed it isn't going to be helpful any more. I appreciate your previous help. I found other workarounds for this. I guess we'll just let it go. – Genius