tags:

views:

77

answers:

3

Hello,

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]
FROM (
    SELECT [t0].[Selected], [t0].[CreateDate]
    FROM [dbo].[A] AS [t0]
    WHERE ([t0].[Selected] = 1)

    UNION ALL

    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.

A: 

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)

vlad
+3  A: 

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

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

And should be:

Coll
   .Where(a => a.Selected)
   .Concat(
       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)
Reed Copsey
my assumption was that he simplified the code in the example, but you're quite right.
vlad
I split in two case because i want the items "Selected" first in the collection resulted
Deumber
@Deumber: I edited to show you a better option for that,as well ;)
Reed Copsey
A: 

Untested, but you could try:

query1.Concat(query2).OrderBy(selector)

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);
Marc Gravell