如何使用linq orderby函数按照以下顺序对此表行进行排序:
答案 0 :(得分:1)
我想这就是你想要做的事情:
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"));
答案 1 :(得分:1)
实际上可以创建没有联合的单个查询:
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 to Objects,那么只需提供自定义比较器,或覆盖对象的Equals和GetHashCode。