c# – Problem using linq to concat two collections – Education Career Blog

public class A
   public bool Selected;
   public DateTime CreateDate;

I have a collection of A, let call it Coll, i want to do:

Coll.Where(a => a.Selected).Concat(Coll.Where(a => !a.Selected)
.OrderBy(a => a.CreateDate))

This look great, but the result is the same if i remove the order part, because Linq is generating the following string

SELECT t2.Selected, t2.CreateDate
    SELECT t0.Selected, t0.CreateDate
    FROM dbo.A AS t0
    WHERE (t0.Selected = 1)


    SELECT t2.Selected, t2.CreateDate
    FROM dbo.A AS t1
    WHERE (NOT (t1.Selected = 1)) 
AS t2

As you can see the OrderBy dissapear in the sql sintax generated by linq, if someone could help with this would be great.


You have a parenthesis mismatch, as pointed out by vlad. This is easier to see if you split the lines:

   .Where(a => a.Selected)
        Coll.Where(a => !a.Selected).OrderBy(a => a.CreateDate)

And should be:

   .Where(a => a.Selected)
       Coll.Where(a => !a.Selected)
   .OrderBy(a => a.CreateDate)

However, this makes no sense. You’re trying to concat all of the members where a.Selected is true with the ones where it’s false – basically selecting the entire collection. You can just select everything (removing the concat) and get the same result:

Coll.OrderBy(a => a.CreateDate)

Edit in response to comments:

I split in two case because i want the items “Selected” first in the collection resulted

In this case, you should do:

Coll.OrderByDescending(a => a.Selected).ThenBy(a => a.CreateDate)


are you trying to order the entire collection? if so, I think a parenthesis is placed in the wrong spot (fragment below is untested):

Coll.Where(a => a.Selected).Concat(Coll.Where(a => !a.Selected)).OrderBy(a => a.CreateDate)


Untested, but you could try:


as in TSQL it doesn’t make sense to filter just part of a query. If you need the first set first, perhaps introduce a dummy value:

query1.Select(row => new {x=1,row})
  .Concat(query2.Select(row => new {x=2,row}))
  .OrderBy(tmp => tmp.x)
  .ThenBy(tmp => tmp.row.whatever)
  .Select(tmp => tmp.row);

Leave a Comment