Local boot and ICollection

Let's say that I have a class called Stock and that has ICollection virtual prices, which are a historical price set.

If you receive stock and after stocks are materialized, you request Prices, but apply a filter, for example, mystock.Prices.OrderByDescending (px => px.Date) .First (), EF internally loads all prices, and then applies the used filters, since prices can be a large collection, I would really like EF to load a price that matches my criteria. Mostly filtering is applied on the server side, and not on the client side.

Can this be done?

thanks

+2
source share
1 answer

Perhaps, but this method only works if you can assume that Prices really an EntityCollection , and not some other class that also implements ICollection . I am not sure if this is true in all supported EF scripts. To use the EntityCollection CreateSourceQuery function.

 ((EntityCollection<Price>)stock.Prices).CreateSourceQuery().OrderByDescending(price => price.Date).First(); 

If this does not work for you, another possibility could be to return to the context and query from there:

 (from price in context.Prices where price.StockId == stockId orderby price.Date descending select price).First(); 
+2
source

Source: https://habr.com/ru/post/1446486/


All Articles