LINQ为了通过布尔 [英] Linq order by boolean
问题描述
我已经得到了我想要f.bar,这是一个字符串命令LINQ查询,但我也想通过f.foo来订购吧,这是一个布尔字段,第一。像下面的查询。
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)
虽然编译如下预期这是行不通的。它只是由f.bar忽略布尔领域的订单。
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?
感谢
推荐答案
这应该做工精细 - 它应当责令有实体假
foo的值,然后再那些具有真正
foo的值。
That should work fine - it should order the entities with a false
foo value first, then those with a true
foo value.
这在LINQ肯定工程的对象 - 这LINQ提供你实际使用
That certainly works in LINQ to Objects - which LINQ provider are you actually using?
下面是一个LINQ到对象例子确实的工作方式:
Here's a LINQ to Objects example which does work:
using System;
using System.Linq;
public static class Test
{
public static void Main()
{
var data = new[]
{
new { x = false, y = "hello" },
new { x = true, y = "abc" },
new { x = false, y = "def" },
new { x = true, y = "world" }
};
var query = from d in data
orderby d.x, d.y
select d;
foreach (var result in query)
{
Console.WriteLine(result);
}
}
}
这篇关于LINQ为了通过布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!