Linq order by ascending sort by descending

Linq order by ascending sort by descending

对不起标题,但我不知道如何写得更好。我会尝试 post.

当我尝试使用带有 orderby 的 linq 从数据库中获取值时,发生了一些奇怪的事情。让我们看一下 4 个查询:

//1
var badAsc = new ConcurrentBag<int>((from x in se.produkts orderby x.numerProduktu select x.numerProduktu));
//2                        
var bagDesc = new ConcurrentBag<int>((from x in se.produkts orderby x.numerProduktu descending select x.numerProduktu));
//3
var listAsc = (from x in se.produkts orderby x.numerProduktu select x.numerProduktu).ToList();
//4
var listdesc = (from x in se.produkts orderby x.numerProduktu descending select x.numerProduktu).ToList();

我们得到了 2 ConcurrentBags<int> 和 2 List<int>。我期望的是 1 和 3 是一样的,2 和 4 也是一样的。检查我得到的值:

ConcurrentBag<int> 的升序实际上是降序。在 Microsoft 网站上我们可以看到,当排序无关紧要时 ConcurrentBag 很好,但正如我们在 bagDesc 中看到的那样,排序会保留。为了表明我在数据库中没有任何奇怪的东西,我还制作了两个 List<int>,其中排序保持原样。

在我的数据库中执行 select * from produkt 给我的值排序为 listAscbagDesc.

数据库是 mssql 2014 并且 numerProduktu 是此 table 中的主键。

有人知道那里发生了什么吗?

here

The ConcurrentBag appears to be implemented as a stack rather than a queue. That is, the last item added is the first item removed. I wouldn't count on that, though.

所以它们是以相反的顺序添加的。但是,顺序在 ConcurrentBag 中并不意味着可靠一致,因此不能保证始终如此,尤其是在被多个线程访问时。

如果您关心保持输入的原始顺序,那么您可能需要 ConcurrentQueue