110,500
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 查询所有application
/// </summary>
/// <returns></returns>
public List<TB_Application> Get_Application(string Project1, string Project2, string Component1, string Component2)
{
List<TB_Application> list = null;
if (Project1 != null && Project1 != "")
{
list = (from i in context.TB_Application where i.Project1.Contains(Project1) select i).ToList();
}
if (Project2 != null && Project2 != "")
{
list = (from i in context.TB_Application where i.Project2.Contains(Project2) select i).ToList();
}
if (Component1 != null && Component1 != "")
{
list = (from i in context.TB_Application where i.Component1.Contains(Component1) select i).ToList();
}
if (Component2 != null && Component2 != "")
{
list = (from i in context.TB_Application where i.Component2 == Component2 select i).ToList();
}
if (Project1 != null && Project1 != "" && Component1 != null && Component1 != "" && Component2 != "" && Component2 != null)
{
list = (from i in context.TB_Application where i.Project1 == Project1 && i.Project2 == Project2 && i.Component1 == Component1 && i.Component2 == Component2 select i).ToList();
}
return list;
}
//条件越多,我判断越多,还要追加,算起来几十次判断,有什么办法像sql语句一样追加条件!
UserEntity user=new UserEntity();
OQL q=new OQL(user);
if(condition1)
q.Condition.Add(user.Age,">=",20);
if(condition2)
q.conditon.Add(user.Class,"=","2年级");
q.Select();
//也可以这样
//q.Select(user.ID,user.Name).Where(user.Class);
var list=EntityQuery<UserEntity>.QueryList(q);
//方法和所在的类都必须为静态,以下只是简单演示,func也可以换成一般的参数形式
public static IEnumrable<TB_Application> Get_Application(this IEnumrable<TB_Application> source, Func<string, bool> func)
{
if(source == null) throw new ArgumentNullException("source");
return source.Where(func);
}
public Func<TB_Application, bool> GetPredicate(string Project1, string Project2)
{
Func<TB_Application, bool> func = null;
if(!string.IsNullOrEmpty(Project1))
{
func = a => a.Project1.Countains(Project1);
}
if (!string.IsNullOrEmpty(Project1))
{
func = a => a.Project2.Countains(Project2);
}
....
return func;
}
//调用
var alist = new List<TB_Application>().Get_Application(GetPredicate(pro1,pro2));
//方法和所有类都必须为静态,以下只是简单演示,func也可以换成一般的参数形式
public static IEnumrable<TB_Application> Get_Application(this IEnumrable<TB_Application> source, Func<string, bool> func)
{
if(source == null) throw new ArgumentNullException("source");
return source.Where(func);
}