Linq order by boolean

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

Leave a Comment