作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我必须使用 LINQ 选择不同的 1000 条记录。但是当我看到生成的查询需要 1000 条记录并对结果应用 distinct。
IQueryable<TestClass> resultSet = (from w in ......).Distinct().Take(1000);
我的 TestClass 会是什么样子,
public TestClass
{
public string TestPRop { get; set; }
//..has some 20 properties
}
有什么方法可以解决这个问题,将 distinct 应用于结果集,然后从不同的结果集中取 1000?
最佳答案
Distinct 将在拍摄前处理。仔细检查您的 Distinct 是否正常运行。这是一个工作示例:
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`
我怀疑您的问题与将 distinct 与 SQL 结合使用有关。如果是这样,您还可以使用分组来获得您想要的结果。
var distinctById = from o in query
group o by o.Id into uniqueIds
select uniqueIds.FirstOrDefault();
关于c# - linq query Take() 优先于 Distinct()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32360015/
我是一名优秀的程序员,十分优秀!