Linq-to-SQL EntitySet Is Not IQueryable — Any Workarounds?

后端 未结 2 2148
情话喂你
情话喂你 2021-02-15 15:14

When you query an EntitySet property on a model object in Linq-to-SQL, it returns all rows from the entityset and does any further querying client-side.

This is confir

相关标签:
2条回答
  • 2021-02-15 15:54

    I had a similar problem: How can I make this SelectMany use a join. After messing with LINQPad for a good amount of time I found a decent workaround. The key is to push the EntitySet you are looking at inside a SelectMany, Select, Where, etc. Once it's inside that it becomes an Expression and then the provider can turn it into a proper query.

    Using your example try this:

    var query = from c in Children
                where c == myChild
                from p in c.Parents
                where p.Age > 35
                select p;
    

    I'm not able to 100% verify this query as I don't know the rest of your model. But the first two lines of the query cause the rest of it to become an Expression that the provider turns into a join. This does work with my own example that is on the question linked to above.

    0 讨论(0)
  • 2021-02-15 15:56

    An EntitySet is just a collection of entities. It implements IEnumerable, not IQueryable. The Active Record pattern specifies that entities be directly responsible for their own persistence. OR mapper entities don't have any direct knowledge of the persistence layer. OR Mappers place this responsibility, along with Unit Of Work, and Identity Map responsibilities into the Data Context. So if you need to query the data source, you gotta use the context (or a Table object). To change this would bend the patterns in use.

    0 讨论(0)
提交回复
热议问题