【发布时间】:2014-01-27 19:31:12
【问题描述】:
这个 LINQ:
public IEnumerable<InventoryItem> GetDepartmentRange(double deptBegin, double deptEnd)
{
// Break the doubles into their component parts:
int deptStartWhole = (int)Math.Truncate(deptBegin);
int startFraction = (int)((deptBegin - deptStartWhole) * 100);
int deptEndWhole = (int)Math.Truncate(deptEnd);
int endFraction = (int)((deptBegin - deptEndWhole) * 100);
return inventoryItems.Where(d => d.dept >= deptStartWhole).Where(e => e.subdept >= startFraction)
.Where(f => f.dept <= deptEndWhole)
.Where(g => g.subdept >= endFraction)
.OrderBy(o => o.dept)
.OrderBy(s => s.subdept);
}
...当我输入时返回预期的数据:
http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/79.99/
...即(子集):
<InventoryItem>
<Description>LID - Blank & Ten PLU</Description>
<ID>110</ID>
<OpenQty>0</OpenQty>
<UPC>110</UPC>
<UnitCost>4</UnitCost>
<UnitList>5</UnitList>
<crv_id>0</crv_id>
<dept>2</dept>
<pksize>1</pksize>
<subdept>10</subdept>
<upc_pack_size>1</upc_pack_size>
<vendor_id>LOCATIONID</vendor_id>
<vendor_item/>
</InventoryItem>
<InventoryItem>
<Description>BLT 6PK LNNR</Description>
<ID>01820000978</ID>
<OpenQty>0</OpenQty>
<UPC>01820000988</UPC>
<UnitCost>19.45</UnitCost>
<UnitList>11.99</UnitList>
<crv_id>0</crv_id>
<dept>10</dept>
<pksize>6</pksize>
<subdept>10</subdept>
<upc_pack_size>1</upc_pack_size>
<vendor_id>CLAREROSE</vendor_id>
<vendor_item/>
</InventoryItem>
<InventoryItem>
<Description>LID - Eleven & Eleven PLU</Description>
<ID>111</ID>
<OpenQty>0</OpenQty>
<UPC>111</UPC>
<UnitCost>4</UnitCost>
<UnitList>5</UnitList>
<crv_id>0</crv_id>
<dept>2</dept>
<pksize>1</pksize>
<subdept>11</subdept>
<upc_pack_size>1</upc_pack_size>
<vendor_id>LOCATIONID</vendor_id>
<vendor_item/>
</InventoryItem>
...但是数据不是按部门和部门排序的(它从部门 2、部门 10 到部门 10、部门 10、部门 2、部门 11。为什么不按 LINQ 排序?怎么能我按部门和子部门排序?
【问题讨论】:
标签: c# linq ienumerable linq-to-objects