简体   繁体   中英

LINQ Select Statement making many SQL Calls

I am trying to run a Select on an IQUeryable linked to a database query. Its working correctly, but for all the properties being selecte-d, its running a seperate query.

My code looks something like this

IQueryable<MyDataSource> data = [Some Complicated Query I've been Building Up];

var results = data.Select(d => new 
{
A = d.A,
B = d.B,
C = d.C

}).Take(100).ToArray();

Now, this is taking ages, even though the actual Query isn't taking that long. When I ran an SQL profiler on it, I'm finding out that its running a different SQL select procedure for each property I'm selecting - for each entity I'm returning (so in the above example around 300 different queries, as well as the actual first query to perform the filtering).

I'm quite sure I'm doing something wrong here, what is it? I'm expecting it to run a single large query - which selects the right columns from the datasource (You know Select top 100 dA, dB, dC from [bla bla] ), not all this mess.

You can influence the lazy/eager loading using DataLoadOptions

This will force eager loading for B

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<a>(a => a.B);
dc.LoadOptions = options;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM