Текучая абстракция в LINQ к SQL EntitySet

В JQuery можно звонить

$("form:first").trigger("submit")

, не знают, намного лучше ли это. Я думаю form.submit (); довольно универсально.

6
задан GEOCHET 26 March 2013 в 23:25
поделиться

3 ответа

You just cannot do that, sadly. Collection properties generated for LINQ to SQL entity classes are not IQueryable; therefore, any queries performed on them will be using LINQ to Objects. This is by design. As you rightly note yourself, to get efficient query you have to query over Transactions taken from your DataContext, but you don't have one in your property gettor.

At this point your options are either:

  • Make it a method which takes a DataContext as argument; or
  • Use reflection hackery to retrive the context - entity itself doesn't store it, but EntitySets on it do, albeit indirectly - naturally this is version specific, prone to breakage, etc.

So far as I know, Entity Framework does not have this limitation, since its collection properties are ObjectQuery - which is IQueryable.

6
ответ дан 10 December 2019 в 02:50
поделиться

What is the type of Transactions in the first example?

Remember you are using Extension Methods. The Linq Extension Methods that are used are dependant on the interface Transactions implements:

  • IQueryable would be linq-to-sql or linq-to-entities or ...
  • IEnumerable will give you linq-to-objects.

Edit:

This is the fingerprint of the EntitySet type:

public sealed class EntitySet<TEntity> : IList, 
    ICollection, IList<TEntity>, ICollection<TEntity>, IEnumerable<TEntity>, 
    IEnumerable, IListSource
where TEntity : class

To answer your questions:

  1. Transactions does not implement IQueryable so it is the correct behaviour
  2. Your account class will need to be able to reference the Transactions Table object
3
ответ дан 10 December 2019 в 02:50
поделиться

Переключитесь с использования IEnumerable на IQueryable, и ваш SQL будет оптимизирован так, чтобы получать только по запросу то, что вам нужно.

0
ответ дан 10 December 2019 в 02:50
поделиться
Другие вопросы по тегам:

Похожие вопросы: