LINQ to Entities等同于sql“TOP(n)WITH TIES” [英] LINQ to Entities equivalent of sql "TOP(n) WITH TIES"

查看:115
本文介绍了LINQ to Entities等同于sql“TOP(n)WITH TIES”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索 LINQ 相当于 c> WITH TIES 在 sql server 我遇到了一些事情,这些事情无法推动,这是有用的。



我知道这个问题之前被问过,并且有一个可以接受的答案,但是它并不适用于 / em>的。 GroupBy()的解决方案不符合 TOP(3)WITH TIES 的预期结果,考虑到一个数据集,的 {3 2 2 1 1 0} 结果集将为 {3 2 2 1 1} 使用以下样本数据(取自 {3 2 2}



< /stackoverflow.com/questions/22010659/select-top-3-unexpexted-result/22010812#22010812\">这个问题)

  CREATE TABLE Person 

Id int primary key,
名称nvarchar(50),
分数float


INSERT INTO Person VALUES(1,'Tom',8.9)
INSERT INTO Person VALUES(2,'Jerry',8.9)
INSERT INTO Person VALUES(3,'Sharti',7)
INSERT INTO Person VALUES(4,'Mamuzi',9)
INSERT INTO Person VALUES(5,'Kamala',9)

传统 OrderByDescending(p => p.Score).Take(3)将导致: Mamuzi 卡马拉和 Tom Jerry )之一,应该包括 BOTH



我知道没有内置的等价物,我已经找到了一种方法来实现它。我不知道这是否是最佳的方法,并为其他解决方案开放。

解决方案

  var query =(from q in list.OrderByDescending(s => s.Score).Take(3).Select(s => s.Score).Distinct()
from i in列表
其中q == i.Score
选择i).ToList();

编辑:



@Zefnus

我不知道你想要什么顺序,但是要改变订单,您可以在 select i ToList()



我没有可能检查我的linq子句将产生什么样的sql语句。但你的答案好多了,我想。你的问题也很好。我从来没有想过与linq的关系。 ;)



基本上,它只取得第一个列表中的前3名,并将其与整个列表进行比较,我只获取等于第一个列的得分的分数列表。


I have been searcing for LINQ equivalent of WITH TIES in sql server lately, I came across a couple things, which couldn't proove to be useful.

I know this question was asked before and has an accepted answer, but it doesn't work the way with ties does. The solution using GroupBy() doesn't result as expected for TOP(3) WITH TIES considering a data set consisting of {3 2 2 1 1 0} the result set will be {3 2 2 1 1} where it should be {3 2 2}

Using the following sample data (taken from this question):

CREATE TABLE Person
(
    Id int primary key,
    Name nvarchar(50),
    Score float
)    

INSERT INTO Person VALUES (1, 'Tom',8.9)
INSERT INTO Person VALUES (2, 'Jerry',8.9)
INSERT INTO Person VALUES (3, 'Sharti',7)
INSERT INTO Person VALUES (4, 'Mamuzi',9)
INSERT INTO Person VALUES (5, 'Kamala',9)

Traditional OrderByDescending(p => p.Score).Take(3) will result with: Mamuzi, Kamala and one of Tom (or Jerry) where it should include BOTH

I know there is no built-in equivalent of it and i've found a way to implement it. I don't know if it is the best way to do it and open for alternative solutions.

解决方案

var query = (from q in list.OrderByDescending(s => s.Score).Take(3).Select(s => s.Score).Distinct()
             from i in list
             where q == i.Score
             select i).ToList();

Edit:

@Zefnus

I wasn't sure in which order you wanted it but to change the order you can put a OrderBy(s => s.Score) between select i and ToList()

I don't have the possibility to check what sql statement my linq clause would produce. But your answer is much better i think. And your question was also really good. I never thought about top with ties in linq. ;)

Basically it only takes top 3 scores from the first list and compares them with the whole list and i takes only those scores which are equal to the scores of the first list.

这篇关于LINQ to Entities等同于sql“TOP(n)WITH TIES”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆