简体   繁体   中英

Is there a difference between these two Search Statements Entity Framework

I have a situtation in which I need to get the top 15 rows from Table where the iFlag column is marked 0.

So i have these two statements:

var t = dbContext.UnProcessedLogs.Take(15).Where(up => up.iFlag == 0).ToList();


var unProcessedlogs = dbContext.UnProcessedLogs.Where(up => up.iFlag == 0).Take(15).ToList(); 

What is the difference between the two statements?

Will the first one work if the top 15 rows have iFlag marked as 1

First version : Take me 15 item, filter on them.

Second version : Take me all item, filter on them, take only 15 of the filtered items.

No, it's not the same.

Yes, they will "work", but won't do the same.

Will the first one work if the top 15 rows have iFlag marked as 1

this will just return an empty resultset.

This is not an error, it's just probably not what you want !

The first one will:

  1. Take the first 15 rows of UnProcessedLogs
  2. Then filter those down to those with iFlag == 0 (which can be less than 15 obviously)

The second one will:

  1. Filter all the rows of UnProcessedLogs down to those with iFlag == 0
  2. Take the first 15 of those

If the table have 30 rows, where every other row have iFlag == 0 and the ones inbetween have iFlag == 1 , then:

  1. The first query will return 7-8 rows (of the first 15 in the table, pick the ones with iFlag == 0 )
  2. The second query will return all 15 with iFlag == 0

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