我可以一起使用两个Orderby条件吗 [英] Can I Use Two Orderby Condition Together
本文介绍了我可以一起使用两个Orderby条件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道有什么方法可以通过new id()使用order和通过< column name =">
i want to know is there any way to used orderby new id() and order by <column name=""> with together????
推荐答案
您可以按两列进行排序,例如select * from table1 order by colA,colB
You can order by two columns for e.g. select * from table1 order by colA,colB
我认为您应该更具体一点^ _ ^
C#linq
I think you should be a bit more specific ^_^
C# linq
private int TypeValue(string type){
switch(type.ToLower()){
case "bird":
return 0;
case "flower":
return 1;
case "animal":
return 2;
case "vegitable":
return 3;
default:
return 100;
}
}
....
list = list.OrderBy(i=>TypeValue(i.type)).ThenBy(i=>i.newId);
//paging
list = list.skip(pageIndex * pageSize).take(pageSize);
c#DataTable
c# DataTable
DataTable dt= new DataTable();
foreach (DataRow row in DT.Rows){
row("CalculatedColumnName") = TypeValue(row.type);
}
DataView dv = new DataView(dt);
dv.Sort = "CalculatedColumnName, newIdASC";
//paging
var paging = dv.AsEnumerable().skip(pageIndex * pageSize).take(pageSize)
SQL
Sql
Select *,
case type
when 'bird' then 0
when 'flower' then 1
when 'animal' then 2
when 'vegitable' then 3
end as TypeSort
from list
order by
case type
when 'bird' then 0
when 'flower' then 1
when 'animal' then 2
when 'vegitable' then 3
end
-- Yes you do have to type it out again (without the name), unless you use common table expressions (cte) but this is simpler
, newId
带CTE的SQL
您将需要将其创建为存储过程
SQL with CTE
You will need to create this as a stored procedure
Sql
<pre lang="SQL">
with firstCTE as ( -- just to get the data and sort column
Select *,
case type
when 'bird' then 0
when 'flower' then 1
when 'animal' then 2
when 'vegitable' then 3
end as TypeSort
from list
),
secondCTE as ( -- to set up the RowNumber
select * ,
ROW_NUMBER() OVER (ORDER BY TypeSort, newId) AS RowNumber
-- this will give the items a RowNumber in order of the sort
from firstCTE
)
select * from secondCTE
where RowNumber >= (@PageStart * @PageSize) AND RowNumber < ((@PageStart+1) * @PageSize)
order by RowNumber
--This is very inexpensive as the cte's are like views and won't select any data until the last 'select'
javascript
javascript
var sortbyVat= function(a, b){
a= a.VatRate;
b= b.VatRate;
if(a== b) return 0;
return a> b? 1:-1;
}
var sortbyPriceAndVat= function(a, b){
a= a.Price;
b= b.Price;
if(a== b) return sortbyVat(a,b);
return a> b? 1:-1;
}
...
array.sort(sortbyPriceAndVat);
更新了c#以供特定用途
updated c# for specific use
这篇关于我可以一起使用两个Orderby条件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文