62,046
社区成员
发帖
与我相关
我的任务
分享
string sql="select * from table where 1=1 and name='jack'";
if(id==0)
sql=sql+" and id !=-1";
else
sql=sql+" and id in(1,2,3)";
if(keyword!=null)
{
string [] list=keyword.split(' ');
for(int i=0;i<list.length;i++)
{
sql=sql+" and keyword like '%'"+list[i]+"'%'";
}
}
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
if(keyword!=null)
{
string [] list=keyword.split(' ');
for(int i=0;i<list.length;i++)
{
sql=sql+" and keyword like '%'"+list[i]+"'%'";
}
}
========
if(keyword!=null)
{
string [] list=keyword.split(' ');
foreach(string str in keyword.split(' '))
{
query=query.Where(t=>t.keyword.Contains(str);
}
}
string sql="select * from table where 1=1 and name='jack'";
if(id==0)
sql=sql+" and id !=-1";
else
sql=sql+" and id in(1,2,3)";
========
var query=from t in table
where t.name=='jack'
select t;
if(id==0)
query=query.Where(t=>t.id !=-1);
else
query=query.Where(t=>new int[]{1,2,3}.Contains(t.id));