高分求助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类该杂写,我觉得这个是个操作符重载的问题,不过还是有些里不清楚思路啊~! c#安装程序中打包msde 查询一串id是否在表中的语句怎么写? 求用winfrom实现的类似豌豆荚通讯录的列表...急!!! GridView添加数据 哪位有ibaits.net和spring.net整合的c#的例子,或者帮助 C# 如何验证用户输入的字符串含有ascii码? 求救! 广告轮换问题 DataBinder.Eval(Container.DataItem,"NewsTitle")字符缩短的问题 关于_dopostback函数 进度条颜色如何改变 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;
}
//...
}
看看数据库操作类