高分求助C#中自定义类型问题 各位大虾,小弟最近想写一个方法,方法的的声明我想是这样的,比如 public void Func(CustomType value){};但是我想在调用此方法时这样,this.Func("A" == "B");这样CustomType这个类我该怎么写啊?在线等啊,急! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void Func(bool value){ Func("A" == "B");} 泛型方法public static void Func<T>(T t) where T : new(){} 我主要是想在实现数据库中的Where条件,如下:public void Func(CustomType value){ string filter=value.ToString(); string SQL="SELECT * FROM table WHERE "+filter;}而调用的时候,Func("A" == "B");第一个"A"我想是个数据库字段,第二个是值,这样Func(CustomType value)方法中的SQL变量就可以成为"SELECT * FROM table WHERE A= 'B' " 直接字符串类型string str="A='B'"; 没看懂你意思,猜测这样?public string CreateWhere(Dictionary<string,string> filter){ string SQL = "SELECT * FROM table WHERE "; StringBuilder builder = new StringBuilder(); foreach (string key in filter.Keys) { builder.Append(key + "='" + filter[key] + "'"); } return SQL + builder.ToString();}//调用Dictionary<string, string> filter = new Dictionary<string, string>();filter.Add("姓名", "飞雪孤风");string sql_command = CreateWhere(filter); // method 1 ?void Func(Predicate<DataRow> filter){ DataTable table = ExecSql("select * from table"); foreach(DataRow row in table.Rows) { if(filter(row)) { //... } }}// method 2 ?public void Func(string filter){ string filter=value.ToString(); string SQL="SELECT * FROM table WHERE "+filter;} CustomType得和select * 出来的字段一样然后直接把数据放在List<CustomType>里返回,SQL语句可以写成public List<CustomType > Func(String value){if(filter!=""){string SQL="SELECT * FROM table WHERE 1=1 "+" and "+filter;}//...} List<object>,Dictionary<object,object>保存条件遍历看看数据库操作类 7楼说的是点意思了,可是我不关是再调用的时候只用Func("A" == "B")啊,我还想用Func("A" >= "B")或者Func("A" <= "B")之类的操作符,还有类似的like '%%'这种模糊的Where条件该杂办啊? 9楼有点不明白意思,我是想问那个CustomType类该杂写,我觉得这个是个操作符重载的问题,不过还是有些里不清楚思路啊~! 无限级扩展 有点难度的问题:关于改变控件已有的对象类型 【100分】这样的功不知能不能实现 格式化字符没起作用 C#编程 winform 大家帮忙,关于使用 DllImport 调用一个 dll 窗体继承问题 如何实现Smart Client 初学C#面向对象 不知道写的规不规范算不算面向对象 求指点 多线程下载文件的问题 winform 和 cookies 这段代码错在哪里?
{
Func("A" == "B");
}
public static void Func<T>(T t) where T : new()
{}
public void Func(CustomType value)
{
string filter=value.ToString();
string SQL="SELECT * FROM table WHERE "+filter;
}
而调用的时候,Func("A" == "B");第一个"A"我想是个数据库字段,第二个是值,
这样Func(CustomType value)方法中的SQL变量就可以成为
"SELECT * FROM table WHERE A= 'B' "
{
string SQL = "SELECT * FROM table WHERE ";
StringBuilder builder = new StringBuilder();
foreach (string key in filter.Keys)
{
builder.Append(key + "='" + filter[key] + "'");
}
return SQL + builder.ToString();
}
//调用
Dictionary<string, string> filter = new Dictionary<string, string>();
filter.Add("姓名", "飞雪孤风");
string sql_command = CreateWhere(filter);
void Func(Predicate<DataRow> filter)
{
DataTable table = ExecSql("select * from table");
foreach(DataRow row in table.Rows)
{
if(filter(row))
{
//...
}
}
}// method 2 ?
public void Func(string filter)
{
string filter=value.ToString();
string SQL="SELECT * FROM table WHERE "+filter;
}
public List<CustomType > Func(String value)
{
if(filter!="")
{
string SQL="SELECT * FROM table WHERE 1=1 "+" and "+filter;
}
//...
}
看看数据库操作类