Reputation: 87
I'm trying to create a method that will take in a Func<Object1, bool>
as a where clause and perform a Select on it as a subquery. This seems to be causing an error on the IQueryable
where it cannot cast it to an expression to be parsed.
using System.Linq
using System.Data.Entity;
public IEnumerable<Object2> CreateObject2List<T> (Expression<Func<T, Object1> whereClause)
{
var parentQuery = (from r in dataSet.SomeObject
select new Object1 { Value1 = r.value1, Value2 = r.Value2 }
var subQuery = parentQuery.Where(whereClause)
.Select<Object1, Object2>( o => new Object2{
Value3 = Value1 + Value2,
Value4 = Value1 - Value2
})
return subQuery.ToList();
}
The problem is occurring on the subQuery. It states:
'IQueryable' does not contain a definition for 'Select' and the best extension method overload
'Queryable.Select<Object1, Object2> (IQueryable<Object1>, Expression<Func<Object1, Object2>>)'
requires a receiver of type 'IQueryable'.
I don't understand how it's not able to recognize that I've already provided an expression in the form of the lambda on the subquery. I made sure that I am using both System.Linq and System.Data.Entity. The delegate type that it is asking for is already there, so why can it not compile?
Also, for clarity, I am doing this because there are other sub-queries to be performed later, this is not a 1-to-1 snippet. I'm going for extensibility on the method call as a service.
Any help would be appreciated, and I can provide more detail as needed, thank you!
Upvotes: 2
Views: 4983
Reputation: 15663
You most likely need
Expression<Func<Object1, bool>>
And replace .Select<Object1, Object2>(
with .Select(
, as mentioned in comments
Upvotes: 3