Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.AsExpandable in Linq to Entity

In Linq to Entity, what does .AsExpandable() exactly do? Where and why to use it? Does it include all the related entities into query for lazy loading?

like image 427
Jitendra Gupta Avatar asked Jun 05 '15 13:06

Jitendra Gupta


People also ask

How do I use asexpandable in Entity Framework?

Entity Framework's query processing pipeline cannot handle invocation expressions, which is why you need to call AsExpandable on the first object in the query. By calling AsExpandable, you activate LINQKit's expression visitor class which substitutes invocation expressions with simpler constructs that Entity Framework can understand.

How does asexpandable work in linqkit?

By calling AsExpandable, you activate LINQKit's expression visitor class which substitutes invocation expressions with simpler constructs that Entity Framework can understand. For more details I would recommend read from the author of LinqPad There is no implicit conversion from a method group to an Expression (of a corresponding delegate type).

How do I execute a LINQ query against the Entity Framework?

To execute a LINQ to Entities query against the Entity Framework, the LINQ query must be converted to a command tree representation that can be executed against the Entity Framework. LINQ to Entities queries are comprised of LINQ standard query operators (such as Select, Where, and GroupBy) and expressions (x > 10, Contact.LastName, and so on).

What are the different types of LINQ to entities queries?

LINQ to Entities queries can be composed in two different syntaxes: query expression syntax and method-based query syntax. Query expression syntax and method-based query syntax are new in C# 3.0 and Visual Basic 9.0. For more information, see Queries in LINQ to Entities.


2 Answers

Entity Framework's query processing pipeline cannot handle invocation expressions, which is why you need to call AsExpandable on the first object in the query. By calling AsExpandable, you activate LINQKit's expression visitor class which substitutes invocation expressions with simpler constructs that Entity Framework can understand.
Josef Albahari

For more details I would recommend read from the author of LinqPad

like image 155
Yuri Avatar answered Oct 17 '22 06:10

Yuri


  • There is no implicit conversion from a method group to an Expression<TDelegate> (of a corresponding delegate type TDelegate).
  • But there is an implicit conversion from a method group to a delegate of a matching signature. Therefore only the IEnumerable overload matches.

Of course, that's not to say that you need to use a lambda. Just write this:

ctx.Set<Person>().AsExpandable().Where(ByName);

Since you're passing in an expression (ByName is, after all, an Expression<Person, bool> already, which is exactly what Queryable.Where<Person> requires) this will evaluate as a query, not in linq to objects.

like image 2
Freddy Jose Chirinos Moròn Avatar answered Oct 17 '22 08:10

Freddy Jose Chirinos Moròn