2

I am using .AsExpandable() extension of LinqKIT to dynamically generate Where expression.

For example,

Expression<Func<Asset, bool>> assetNamePredicate = m => m.AssetName == "abc";
Expression<Func<Asset, bool>> assetPurchaseDateFromPredicate = m => m.PurchaseDate > DateTime.Now.AddDays(-10);
Expression<Func<Asset, bool>> whereExpression = t => assetNamePredicate.Invoke(t) && assetPurchaseDateFromPredicate.Invoke(t); 

return new entitiesContainer().Set<Asset>().AsExpandable().Where(whereExpression).ToList<Asset>();

Now the Asset entity contains AssetTypeID which is having a foreign key to ID field of AssetType table. But When I try to write Include statement to include AssetType in the resultset, it doesn't work, and the AssetType property for each Asset entity is null always... however, the same thing works fine if I don't use .AsExpandable() extension.

Any idea of how we can use AsExpandable() extension for resultset to load related entities.

P.S. - my scenario is a bit complicated than the sample code I have provide, so I can't avoid using this extension for sure..

Nirman
  • 6,715
  • 19
  • 72
  • 139

2 Answers2

0

Apparently it is a known issue that the AsExpandable extension method returns a different return type, therefore breaking the Including bit of Entity Framework. There is a workaround for this: projections. If you define a Select statement in which you navigate to your navigation properties, you should be able to include them in your result set.

hbulens
  • 1,872
  • 3
  • 24
  • 45
0

I found solution to this issue - Basically, this requires to create an array of "Include" expressions, and query against them -

For example,

    List<Expression<Func<Asset, object>>> includes = new List<Expression<Func<Asset, object>>>();
    includes.Add(m => m.City);
    includes.Add(m => m.City.Country); 

        return includes
            .Aggregate(query.AsQueryable(), (current, include) => current.Include(include))
            .AsExpandable()
            .Where(whereExpression)
            .ToList<Address>();
Nirman
  • 6,715
  • 19
  • 72
  • 139