开发者

Problem using linq to concat two collections

开发者 https://www.devze.com 2023-01-20 16:34 出处:网络
public class A { public bool Selected; public DateTime CreateDate; } I have a collection of A, let call it Coll, i want to do:
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].[C开发者_C百科reateDate]
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.


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)


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:

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

精彩评论

暂无评论...
验证码 换一张
取 消