简体   繁体   中英

linq query Take() takes precedence over Distinct()?

I have to select distinct 1000 records using LINQ. But when I see the query generated it takes 1000 records and applies distinct over that result.

IQueryable<TestClass> resultSet = (from w in ......).Distinct().Take(1000);

where my TestClass would be like,

public TestClass
{
public string TestPRop { get; set; }
 //..has some 20 properties
}

Any way to solve this to get distinct applied to the resultset and then take 1000 from the distinct result set?

Distinct will be processed before the take. Double check your Distinct is performing correctly. Here's a working example:

var dataset = new int[]
{
    1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10
};

var query = from o in dataset select o;
var result = query.Distinct().Take(6);

// result = `1,2,3,4,5,6`

I suspect your issue is with using distinct with SQL. If that is the case, you can also use grouping to get the result you want.

var distinctById = from o in query
                   group o by o.Id into uniqueIds
                   select uniqueIds.FirstOrDefault();

You are only taking 1000 records and then applying distinct over those 1000 records with you code even though you are doing distinct() before Take().

The best way to accomplish this is to get the 1000 distinct records directly from your SQL query rather than from the code IE

string queryString = "SELECT top 1000 from (select distinct ... from table)";

您可以使用嵌套的linq查询

IQueryable<TestClass> resultSet = from x in ((from w in ......).Distinct()).Take(1000);

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