does "linq where" not resolve properties?
I have an IQueryable (PartHistories) that will return a set of objects. I have an enum called Phase that alters the query, like this (rc is a list):
var query = this.PartHistories;
if ( Phase.Repair == _Phase )
{
query = query.Where( ph => ph.RemovedInRepair == false );
}
else
{
query = query.Where( ph => ph.PartPhaseID != ( int )Phase.Repair );
}
rc.AddRange( query
.ToList( ) );
This works.
Now, the object in question (PartHistory) has a property, PartPhase, which casts PartPhaseID as Phase. This code, however, does NOT work (the change is in the Phase.Repair line):
var query = this.PartHistories;
if ( Phase.Repair == _Phase )
{
query = query.Where( ph => ph.RemovedInRepair == false );
}
else
{
query = query.Where( ph => ph.PartPhase != Phase.Repair );
}
rc.AddRange( query
.ToList( ) );
At the AddRange, the exception is "the member 'PartPhase' is not supported.
Finally, the following code works, but in this case, the Where is the extension of List and (I think) applies after all the PartHistories have been collected:
if( Phase.Repair == _Phase )
{
rc.AddRange( this.PartHistories.ToList( )
.Where( ph => ph.RemovedInRepair == false ) );
}
else
{
rc.AddRange( this.PartHistories.To开发者_JAVA技巧List( )
.Where( ph => ph.PartPhase != Phase.Repair ) );
If this is Linq to SQL, then yes, it does not support custom method calls. It can translate expressions to SQL queries, but doesn't know what happens inside your property. So, custom methods and properties are not convertible to SQL.
If you could make an Expression<Func<PartHistory, PartPhase>>
, which would do the same thing your property does (without calling custom methods), then it would work.
精彩评论