WAQS Querying: Avoid useless data loading using partial entity loading

7 reasons to use WAQS

WAQS documentation

 

As I already explain in the previous post, LINQ To Entities does not support constructor in entity types.

However, it could be very useful to have this feature.

For example, imagine that we have a property Picture in Product entity.

In a screen used to Create / Update an Order, we need to load the products. But we often just need the Id and the Name properties to fill a ComboBox for example.

So in this case, it would definitively be a shame to load the whole Products.

However, because we have a property Product in Order, it could be useful to use the Product class.

 

To do the same with LINQ To Entities, we can get the expected properties using a new type. Then, in memory, we can create some products from these new type instances. Then, we have to attach these products into the context which could be a pain if the context is not empty and already contains some products.

 

LINQ To WAQS allows you to use a constructor on Model types.

 

Like with WAQS method, it generates a type in the server to do the job:

class InheritedProduct : Product 
{
}


Note that when you use and entity constructor, With and Include methods are no more usable in the current version.



However, you can load a calculated property like this:



var orders = await _context.Orders.AsAsyncQueryable().Select(o => 
new Order { Id = o.Id, CustomerName = o.CustomerName }).ExecuteAsync();


// That’s why CustomerName property set (that just throws an InvalidOperationException) is useful.

This entry was posted in 12253, 16868, 7674. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>