开发者

LINQ to Entities pulling back entire table

开发者 https://www.devze.com 2022-12-26 06:20 出处:网络
In my application I\'m pulling back a user\'s \"feed\".This contains all of that user\'s activities, events, friend requests from other users, etc.When I pull back the feed I\'m calling various functi

In my application I'm pulling back a user's "feed". This contains all of that user's activities, events, friend requests from other users, etc. When I pull back the feed I'm calling various functions to filter the request along the way.

        var userFeed = GetFeed(db); // Query to pull back all data
        userFeed = FilterByUser(userFeed, user, db); // Filter for the user
        userFeed = SortFeed(userFeed, page, sortBy, typeName); // Sort it

The data that is returned is exactly what I need, however when I look at a SQL Profile 开发者_如何学CTrace I can see that the query that is getting this data does not filter it at the database level and instead is selecting ALL data in the table(s).

This query does not execute until I iterate through the results on my view. All of these functions return an IEnumerable object.

I was under the impression that LINQ would take all of my filters and form one query to pull back the data I want instead of pulling back all the data and then filtering it on the server.

What am I doing wrong or what don't I understand about the way LINQ evaluates queries?


If GetFeed returns an IEnumerable, FilterByUser will receive an IEnumerable. When it calls some LINQ operator, i.e. Where, it will use the IEnumerable Where, which will start to ask for information, which will eventually download the entire table. Change the type of GetFeed to IQueryable to make sure that IQueryable's LINQ operators are called instead, which will keep delaying the query.

0

精彩评论

暂无评论...
验证码 换一张
取 消