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;
}

时间: 2024-08-27 16:04:26

C#字典转换成where条件的相关文章

C#将字典转换成name=value这种字符串格式

/// <summary> /// 将字典转换成name=value这种字符串格式 /// </summary> /// <param name="dic"></param> /// <returns></returns> public static string DictionaryToStr(IDictionary<string,string> dic) { //使用排序字典 dic = new S

将字典转换成变量, 字符串与列表相互转换

将字典转换成变量: >>> locals().update({'a':1,'b':2}) >>> a 1 >>> b 2 字符串与列表相互转换 str >>>list str1 = "12345" list1 = list(str1) print list1 str2 = "123 sjhid dhi" list2 = str2.split() #or list2 = str2.split(&q

python中将字典转换成定义它的json字符串

Python的字典和JSON在表现形式上非常相似 #这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'], 'sub_dic': { 'sub_str': 'this is sub str', 'sub_list': [1, 2, 3] }, 'end': 'end' } //这是javascript中的一个JSON对象 json_obj = { 'str': 'this is a string',

python 将类对象转换成json

如果将字典转换成json,想必都很熟悉了,如果在进阶点,将class类转换成json对象该如何操作了? 1,先定义一个类 #定义一个Student类 class Student(object): def __init__(self,name,age,score): self.name = name self.age = age self.score = score 2,在实例化Student类,传入3个参数 #实例化这个对象 s = Student('hello',20,80) 3,利用json

字典转模型,模型转字典,将某个类中数组直接转换成模型

/** *  数组中需要转换的模型类 * *  @return 字典中的key是数组属性名,value是数组中存放模型的Class */ - (NSDictionary *)objectClassInArray; 使用实例: @property(nonatomic,strong)NSArray *pic_urls; - (NSDictionary *)objectClassInArray{ return @{@"pic_urls":[StatusPhoto class]}; } NSA

c#将枚举转换成字典集合

枚举在软件开发中的用途 1. 枚举类型(enum type)是具有一组命名常量的独特的值类型. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } 或者:如果只给男赋值,那么女=1 public enum Sex { 男 = 0, 女 } 3. 我们在实际开发中,对于数据库的设计会经常需要很多状态字段(比如性别.审核状态.分类状态等等等等),而这些状态字段的值又只有固定的几个,这个时候我们一般会需要数据字典来维护这些数据.而数据字典该以什么形式存在呢? 以我自己

IOS 把格式化的JSON字符串转换成字典

[html] view plaincopyprint? /*! * @brief 把格式化的JSON格式的字符串转换成字典 * @param jsonString JSON格式的字符串 * @return 返回字典 */ + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString d

把一个类(或者Object)转换成字典

直接上代码:把一个类转换成object,然后在转换成字典 1 internal static IDictionary<string, string> GetDictionary(this object source) 2 { 3 if (source == null) 4 { 5 return new Dictionary<string, string>(); 6 } 7 PropertyDescriptorCollection properties = TypeDescripto

python将字符串转换成字典的几种方法

当我们遇到类似于{'a':1, 'b':2, 'c':3}这种字符串时,想要把它转换成字典进行处理,可以使用以下几种方法: 1. Python自带的eval函数(不安全) dictstr = '{"a":1, "b":2, "c":{"d":1}}' mydict = eval(dictstr) 2.使用 ast 模块的 literal_eval 函数(安全) dictstr = '{"a":1, &quo