【发布时间】:2013-03-04 13:56:56
【问题描述】:
我有 2 个 Lists 返回相同的 Item 。
<foo> 具有 orderType 的专有性,即第一个列表为 0,第二个列表为 1
在第一个列表中,我进行过滤,我必须将第二个列表中的项目添加到受分页限制的结果中。
基本上这是我的最终查询:
var listFoo= QueryList1.Concat(QueryList2); //(IQueriable)
List<foo> listFoo =listFoo.OrderByDescending(r => r.ID)
.ThenBy(d =>d.orderType)
.Skip((currentPageIndex - 1) * pageSize)
.Take(pageSize)
.ToList();
这很好用,因为列表 1 作为主要项目,而列表 2 作为第一个列表的详细信息。此外,我的过滤器应该只适用于第一个列表。但问题来了。如何按日期仅订购第二个列表。我需要列出按日期排序的详细信息。 基本上我需要类似的东西:
List<foo> listFoo =listFoo.OrderByDescending(r => r.ID)
.ThenBy(d =>d.orderType)
.ThenBy(x=>(x.ordertype==1)?x.Date)
.Skip((currentPageIndex - 1) * pageSize)
.Take(pageSize)
.ToList();
编辑:
List 1 :
id =1,ordertype=0,Date = new DateTime(1950,1,4), [0]
id =2,ordertype=0,Date = new DateTime(1950,2,1) [1]
List 2 :
id =1,ordertype=1,Date = new DateTime(1950,1,5), [2]
id =1,ordertype=1,Date = new DateTime(1950,1,2), [3]
id =1,ordertype=1,Date = new DateTime(1950,1,3), [4]
id =1,ordertype=1,Date = new DateTime(1950,1,4) [5]
This should be ordered as follows :
[0],[3],[4],[5],[2],[1]
【问题讨论】:
-
在我看来,您总是可以简单地按 ID、ordertype 和 Date 进行订购。订单类型 0 总是第一个具有相同的 ID。
-
是的......你是对的。这是我将使用的解决方案.. 我会给斯坦利一个正确的答案,以防有人碰到我认为我遇到的问题。 (问题在于定义一个字段)。干杯队友
标签: c# asp.net-mvc asp.net-mvc-3 linq