C#字典转换成where条件
where 1=1 and Dictionary[key1]=Dictionary[value1] and Dictionary[key2]=Dictionary[value3]。。。。
/// <summary>
/// 传入一个字段返回where条件
/// </summary>
/// <param name="where">字段</param>
/// <param name="tableAlias">可以为空</param>
/// <returns></returns>
protected string GetWhere(Dictionary<string, object> where, string tableAlias)
{
string sql = "";
string aliasName = string.Empty;
if (!string.IsNullOrEmpty(tableAlias))
{
aliasName = tableAlias + ".";
}
if (where == null)
{
return sql;
}
string joinList = string.Empty;
foreach (var item in where)
{
if (string.IsNullOrEmpty(sql))
{
sql += " where ";
}
else if (!string.IsNullOrEmpty(sql.Replace("where", "").Trim()))
{
sql += " and ";
}
string speCode = "┝┝┞┞├├┼┽┾┿╀╂┣─┐┎";
if (item.Value == null)
{
sql += "nvl(" + aliasName + item.Key + ",'"+speCode+"') = '"+speCode+"'";
}
else if (string.IsNullOrEmpty(item.Value.ToString()))
{
sql += "nvl(" + aliasName + item.Key + ",'"+speCode+"') = '"+speCode+"'";
}
else if (item.Value is List<string>)
{
List<string> valueList = (List<string>)item.Value;
string valueStr = "";
if (valueList.Count > 1)
{
//sql += " in (" + valueStr + ") ";
int strIndex = 0;
valueList = valueList.Distinct().ToList();
foreach (var str in valueList)
{
if (strIndex > 0)
{
valueStr += " union all ";
}
valueStr += " select '" + str + "' as TEMP" + item.Key + " from dual ";
strIndex++;
}
joinList += " inner join (" + valueStr + ") " + item.Key + "TABLE ON " + aliasName + item.Key +
"=" + item.Key + "TABLE.TEMP" + item.Key;
if (sql.EndsWith("and "))
{
sql = sql.Substring(0, sql.Length - 4);
}
}
else
{
foreach (var str in valueList)
{
valueStr += "'" + str + "',";
}
valueStr = valueStr.TrimEnd(',');
sql += aliasName + item.Key;
sql += " = " + valueStr + " ";
}
}
else if (item.Value is List<int>)
{
List<int> valueList = (List<int>)item.Value;
string valueStr = "";
foreach (var str in valueList)
{
valueStr += str + ",";
}
valueStr = valueStr.TrimEnd(',');
sql += aliasName + item.Key;
if (valueList.Count > 1)
{
sql += " in (" + valueStr + ") ";
}
else
{
sql += " = " + valueStr + " ";
}
}
else if (item.Value is List<decimal>)
{
List<decimal> valueList = (List<decimal>)item.Value;
string valueStr = "";
foreach (var str in valueList)
{
valueStr += str + ",";
}
valueStr = valueStr.TrimEnd(',');
sql += aliasName + item.Key;
if (valueList.Count > 1)
{
sql += " in (" + valueStr + ") ";
}
else
{
sql += " = " + valueStr + " ";
}
}
else if (item.Value is string)
{
sql += aliasName + item.Key + " = '" + item.Value.ToString() + "'";
}
else
{
sql += aliasName + item.Key + " = " + item.Value.ToString();
}
}
if (string.IsNullOrEmpty(sql.Replace("where", "").Trim()))
{
sql += " 1=1 ";
}
//选择是否需要比如分区控制之类的
//if (!where.ContainsKey("F_SITE_ID"))
//{
//sql += CommomBus.GetPatitionFilter(tableAlias);
//}
return joinList + " " + sql;
}
C#字典转换成where条件的更多相关文章
- 字典转换成NSString(NSJson)
//字典转换成字符串 NSDictionary *dict = [NSMutableDictionary dictionary]; NSData *data = [NSJSONSerializatio ...
- python2.7字典转换成json时中文字符串变成unicode的问题:
参考:http://blog.csdn.net/u014431852/article/details/53058951 编码问题: python2.7字典转换成json时中文字符串变成unicode的 ...
- PHP字符串如何转换成if条件语句
例如: $condition = "2 == 2 && 3 == 5"; if ( $condition ){ echo 1; } 怎样把 $condition 转 ...
- Python 字符串、元组、字典转换成列表
- python3 下列表与字典转换
在写爬虫的时候,经常需要处理cookie,requests库里的cookie是dict,但是headers['cookie']却是一个key=value的字符串. 下面是几个用推导式实现的转换函数,供 ...
- python class对象转换成json/字典
# -*- encoding: UTF-8 -*- class Student: name = '' age = 0 def __init__(self, name, age): self.name ...
- C#中服务端接受前端JSON字符串转换成字典集合
我们是否可以把从前端接受的JSON字符串转换成字典集合呢? 比如从前端接收:{'size':'10', 'weight':'10kg'} 在服务端转换成:[{size:"10"}, ...
- python :eval将字符串转换成字典
#将字符串打印成字典 b=''' {'record': {'weight':20,'server':'100.1.7.9','maxconn':50},'backend': 'www.oldboy.o ...
- 把一个类(或者Object)转换成字典
直接上代码:把一个类转换成object,然后在转换成字典 internal static IDictionary<string, string> GetDictionary(this ob ...
随机推荐
- net.sf.json.JSONException: java.lang.reflect.InvocationTargetException Caused by: java.lang.IllegalArgumentException at java.sql.Date.getHours(Unknown Source)
数据库字段类型为Date,转成JSON格式会有问题,解决方案如下: json-lib有一个配置类JsonConfig通过JsonConfig可以注册一个字段处理器实现JsonValueProcesso ...
- 从零开始——PowerShell应用入门(全例子入门讲解)
学习一门技术,不止要会,还要善用,例子就是带你快速入门的最佳利器.本文就是要用例子,不,大量的例子来带你走进PowerShell应用世界. 本文主要介绍一些PowerShell入门的基础知识,对技术小 ...
- python 第五弹
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- [HDU1000] A + B Problem
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...
- Java文件流应用:剪切文件
剪切的实际操作是: 第一步,先复制文件到指定位置, 第二步,删除原文件,这样就实现了剪切 与复制文件相比,多了一步删除文件 注意:删除文件必须在流关闭后,才能删除,否则,删除不了. 实例代码 pack ...
- Python中的模块介绍和使用
在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...
- javascript的运行过程以及setTimeout的运行机制
关于javascript的运行机制大家都应该有所了解了吧,其实javascript是一个单线程的机制,但是因为队列的关系它的表现会让我们感觉是一个多线程的错觉.javascript在运行的时候是这样的 ...
- POI使用:用poi接口不区分xls/xlsx格式解析Excel文档(41种日期格式解析方法,5种公式结果类型解析方法,3种常用数值类型精度控制办法)
一.使用poi解析excel文档 注:全部采用poi接口进行解析,不需要区分xls.xlsx格式,不需要判断文档类型. poi中的日期格式判断仅支持欧美日期习惯,对国内的日期格式并不支持判断,怎么办? ...
- [编织消息框架][netty源码分析]4 eventLoop 实现类NioEventLoop职责与实现
NioEventLoop 是jdk nio多路处理实现同修复jdk nio的bug 1.NioEventLoop继承SingleThreadEventLoop 重用单线程处理 2.NioEventLo ...
- 针对Oracle数据库表中的数据的常见操作
1.查询表中所有数据 select * from 表名; 例:select * from stu; 2.查询的同时修改表中数据 select * from 表名 for update; 例:sele ...