Linq 按布尔值排序

发布于 2024-10-25 23:21:07 字数 247 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弃爱 2024-11-01 23:21:07

这应该可以正常工作 - 它应该首先对具有 false foo 值的实体进行排序,然后对具有 true foo 值的实体进行排序。

这在 LINQ to Objects 中当然有效 - 您实际使用的是哪个 LINQ 提供程序?

下面是一个 LINQ to Objects 示例,它确实可以工作:

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);
        }
    }
    
}

That should work fine - it should order the entities with a false foo value first, then those with a true 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:

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);
        }
    }
    
}
活泼老夫 2024-11-01 23:21:07

只是想这样做,这似乎没有隐式排序。为了更明确,我做了以下操作:

Something.OrderBy(e=>e.SomeFlag ? 0 : 1) 

将某些内容从真到假进行排序。

Just wanted to do this and it seems like something with no implicit ordering. I did the following to be more explicit:

Something.OrderBy(e=>e.SomeFlag ? 0 : 1) 

to sort something true to false.

空气里的味道 2024-11-01 23:21:07

为了更明确地说明所使用的顺序。

Something.OrderBy(e => e.SomeFlag, new BooleanComparer());

public class BooleanComparer : IComparer<bool>
{
    public int Compare(bool x, bool y)
    {
        int p = x ? 1 : 0;
        int q = y ? 1 : 0;
        return p - q; 
    }
}

In order to be more explicit about the order being used.

Something.OrderBy(e => e.SomeFlag, new BooleanComparer());

public class BooleanComparer : IComparer<bool>
{
    public int Compare(bool x, bool y)
    {
        int p = x ? 1 : 0;
        int q = y ? 1 : 0;
        return p - q; 
    }
}
九命猫 2024-11-01 23:21:07

如果您得到 list orderby true ,请尝试以下代码。

db.member.where(x=>x.id==memberId).OrderBy(x=>!x.IsPrimary?1:0).ToList();

Please try following code if you get list orderby true .

db.member.where(x=>x.id==memberId).OrderBy(x=>!x.IsPrimary?1:0).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文