Linq 按布尔值排序
我有一个 linq 查询,我想通过 f.bar(它是一个字符串)排序,但我也想首先通过 f.foo(它是一个布尔字段)排序。就像下面的查询一样。
(from f in foo
orderby f.foo, f.bar
select f)
虽然可以编译,但它并没有按预期工作。它只是通过 f.bar 进行排序,忽略布尔字段。
我知道我很愚蠢,但是我需要做什么才能得到这种行为?
谢谢
I've got a linq query that I want to order by f.bar, which is a string, but I also want to order it by f.foo, which is a boolean field, first. Like the query below.
(from f in foo
orderby f.foo, f.bar
select f)
Although this compiles it doesn't work as expected. It just orders by f.bar ignoring the boolean field.
I'm being daft I know, but what do I need to do to get this behaviour?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以正常工作 - 它应该首先对具有
false
foo 值的实体进行排序,然后对具有true
foo 值的实体进行排序。这在 LINQ to Objects 中当然有效 - 您实际使用的是哪个 LINQ 提供程序?
下面是一个 LINQ to Objects 示例,它确实可以工作:
That should work fine - it should order the entities with a
false
foo value first, then those with atrue
foo value.That certainly works in LINQ to Objects - which LINQ provider are you actually using?
Here's a LINQ to Objects example which does work:
只是想这样做,这似乎没有隐式排序。为了更明确,我做了以下操作:
将某些内容从真到假进行排序。
Just wanted to do this and it seems like something with no implicit ordering. I did the following to be more explicit:
to sort something true to false.
为了更明确地说明所使用的顺序。
In order to be more explicit about the order being used.
如果您得到 list orderby true ,请尝试以下代码。
Please try following code if you get list orderby true .