如何使用linq orderby函数按照以下顺序对此表行进行排序:
发布于 2013-10-29 11:04:55
实际上,可以在没有联合的情况下创建单个查询:
var query =
db.Foos
.OrderBy(f =>
f.BusinessType == "A01 - Production" ? 0 :
f.BusinessType == "A06 - External trade without explicit capacity" ?
(f.InArea == "ME" ? 1 : (f.OutArea == "ME" ? 2 : 6)) :
f.BusinessType == "A02 - Internal trade" ? 3 :
f.BusinessType == "A04 - Consumption" ? 4 : 5)
.ThenBy(f =>
f.BusinessType == "A06 - External trade without explicit capacity" ?
(f.InArea == "ME" ? f.OutArea :
(f.OutArea == "ME" ? f.InArea : "")) : "");
如果您对对象使用Linq,那么只需提供自定义比较器,或者重写对象的等于和GetHashCode。
发布于 2013-10-29 10:30:32
我猜这就是你想要做的
var result =
input.Where(e => e.BusinessType == "A01 - Production")
.Concat(input.Where(e => e.BusinessType == "A06 - External trade without explicit capacity" && e.InArea == "ME")
.OrderBy(e => e.OutArea))
.Concat(input.Where(e => e.BusinessType == "A06 - External trade without explicit capacity" && e.OutArea == "ME")
.OrderBy(e => e.InArea))
.Concat(input.Where(e => e.BusinessType == "A02 - Internal trade"))
.Concat(input.Where(e => e.BusinessType == "A04 - Consumption"));
https://stackoverflow.com/questions/19655411
复制相似问题