Casting in a linq expression

Posts   
 
    
arobadol
User
Posts: 17
Joined: 04-Jan-2007
# Posted on: 30-Apr-2010 22:35:03   

I'm trying to prefilter the queries if the Entity implement a specific interface.

for that I'm using a filter expression that is doing a cast.

I first check if the Entity implement the function. If so I filter on a property of the interface.

Here is my function:

private static DataSource2<TEntity> GetDataSource<TEntity>(ILinqMetaData metadata) where TEntity : class,IEntity2 { if (typeof(IDeactivableEntity).IsAssignableFrom(typeof(TEntity))) { //We entity is deactivable entity, we should only returned the entities which are not deleted. var datasource = metadata.GetQueryableForEntity((int)Enum.Parse(typeof(EntityType), typeof(TEntity).Name)) as DataSource2<TEntity>; return datasource.ToList().Where(x => !(x as IDeactivableEntity).IsDeleted) as DataSource2<TEntity>;

        }
        return metadata.GetQueryableForEntity((int)Enum.Parse(typeof(EntityType), typeof(TEntity).Name)) as DataSource2<TEntity>;
    }

Unfortunately everytime I do a call like: GetDataSource<CountryEntity>().ToList();

Where CountryEntity implement the interface IDeactivableEntity... I get an error.

The error I get is: Value cannot be null. Parameter name: source

Any idea.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-May-2010 05:08:40   

Casting that way is used for Inheritance. The way you are doing, in my opinion will be interpreted as an in-memory function and the framework wont convert it correctly to a sql query. Remember that in-memory functions should be in the last projection of the expression tree. A couple of more IFs should fix your code.

Anyway please post the complete exception stacktrace to know more about the error. Also the LLBLGen runtime library version.

David Elizondo | LLBLGen Support Team