linq order by charindex 排序 按给定字符串顺序排序
2015-06-03 18:47
423 查看
//list=list.OrderBy(ee => SqlFunctions.CharIndex("书记,主任,支部委员,村委委员,系统工作人员", ee.ZhiWu)).ToList(); //linq to sql中报错:只能在linq to entities中调用此方法.
int i = 0;
list = list.OrderBy(ee => { i=Array.IndexOf(new string[] { "书记", "主任", "支部委员", "村委委员", "系统工作人员" }, ee.ZhiWu);if ( i!= -1) { return i; } else { return int.MaxValue; } }).ToList(); //字符串在字符数组中出现的位置,不在数组中的排在最后
扩展: 如果字符串是 村委委员1 那么就不在字符串数组中了会排到后面去(如上图),下面函数解决模糊匹配问题
int PatIndex(string[] array, string value)
{
int index = -1;
for (int i = 0; i < array.Length; i++)
{
//if (value.Contains(array[i]))
if (value.StartsWith(array[i]))//以搜索字符串开头的
{
index = i;
break;
}
}
return index;
}
list = list.OrderBy(ee => { i=PatIndex(new string[] { "书记", "主任", "支部委员", "村委委员", "系统工作人员" }, ee.ZhiWu);if ( i!= -1) { return i; } else { return int.MaxValue; } }).ToList();
补充 sql:case when then
select * from table order by case when cunorju like '%街道%' then 0 when cunorju like '%村委%' then 1 when cunorju like '%居委%' then 2 else 3 end
sql order by charindex(...):
Select * from table order by case when CharIndex(cunorju ,N'街道村委居委')>0 then CharIndex(cunorju ,N'街道村委居委') else 2000000000 end
/*符合的放最前面,不符合的排在2000000000位置*/
int i = 0;
list = list.OrderBy(ee => { i=Array.IndexOf(new string[] { "书记", "主任", "支部委员", "村委委员", "系统工作人员" }, ee.ZhiWu);if ( i!= -1) { return i; } else { return int.MaxValue; } }).ToList(); //字符串在字符数组中出现的位置,不在数组中的排在最后
扩展: 如果字符串是 村委委员1 那么就不在字符串数组中了会排到后面去(如上图),下面函数解决模糊匹配问题
int PatIndex(string[] array, string value)
{
int index = -1;
for (int i = 0; i < array.Length; i++)
{
//if (value.Contains(array[i]))
if (value.StartsWith(array[i]))//以搜索字符串开头的
{
index = i;
break;
}
}
return index;
}
list = list.OrderBy(ee => { i=PatIndex(new string[] { "书记", "主任", "支部委员", "村委委员", "系统工作人员" }, ee.ZhiWu);if ( i!= -1) { return i; } else { return int.MaxValue; } }).ToList();
补充 sql:case when then
select * from table order by case when cunorju like '%街道%' then 0 when cunorju like '%村委%' then 1 when cunorju like '%居委%' then 2 else 3 end
sql order by charindex(...):
Select * from table order by case when CharIndex(cunorju ,N'街道村委居委')>0 then CharIndex(cunorju ,N'街道村委居委') else 2000000000 end
/*符合的放最前面,不符合的排在2000000000位置*/
相关文章推荐
- 2的N次幂表示
- 账户推广流程
- 网页加速的14条法则
- 数理统计中的点估计
- Mac上mariadb的启动与关闭
- oracle vm virtualbox 如何让虚拟机可以上网
- 软件工具之实用软件推荐
- Eclipse快捷键
- iOS模型以及使用
- Extjs中引入JSP页面
- Xcode6 中iphone5s(7.1)模拟器无法全屏
- quartz集群分布式(并发)部署解决方案-Spring
- c++智能指针:auto_ptr shared_ptr
- Mysql 数据库文件存储在哪个目录
- 二层冗余与环路避免基础
- HihoCoder第十四周:无间道之并查集
- Struts2实现用户自行选择语言
- Android更换皮肤解决方案
- "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug
- HihoCoder第十四周:无间道之并查集