linq to sql 问题!

超凡 2011-08-18 10:49:40

/// <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语句一样追加条件!
...全文
140 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
永动bug制造机 2011-08-19
  • 打赏
  • 举报
回复
猫哥[Quote=引用 9 楼 fangxinggood 的回复:]
看看 tim 的 blog:

http://blog.csdn.net/q107770540/article/details/5724013
[/Quote]
超凡 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fangxinggood 的回复:]
看看 tim 的 blog:

http://blog.csdn.net/q107770540/article/details/5724013
[/Quote]

但我不明白的时,假如数据库按第一个条件查询数据有一百万条,再按第二个条件查询时,他数据只有二十万条,按第三个条件查询的时候数据只有一万第!

如果用sql 条件追加他只操作一次数据库就能查询一万条

用linq那他首先查出的是一百万条,再保存到集合,再从集合中查询这一万条数据,效率相比,linq会差远了啊!

请问条件追加,我的理解是不是对的?
超凡 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fangxinggood 的回复:]
看看 tim 的 blog:

http://blog.csdn.net/q107770540/article/details/5724013
[/Quote]

很有帮助 !
超凡 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dongxinxi 的回复:]
把那些经常用到的条件写到自定义的扩展方法里头
[/Quote]

怎样扩展啊?
  • 打赏
  • 举报
回复
把那些经常用到的条件写到自定义的扩展方法里头
超凡 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fangxinggood 的回复:]
List<TB_Application> list = null;

不要定义为 List<T> 定义为 IQueryable<T>

条件可以用 Where 不断拼下去。这相当于 And 连接。如果是 Or 可以用 Unit
[/Quote]

可以帮我写个例子吗? 机器哥!
机器人 2011-08-18
  • 打赏
  • 举报
回复
List<TB_Application> list = null;

不要定义为 List<T> 定义为 IQueryable<T>

条件可以用 Where 不断拼下去。这相当于 And 连接。如果是 Or 可以用 Unit
超凡 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tangxinggang 的回复:]
程序设计的有问题 你实现什么功能
[/Quote]
查询功能!

实现是条件追加!

假如方法中的参数不为空,则追加此条件!
bios8086 2011-08-18
  • 打赏
  • 举报
回复
我来关注下!
请问下楼主 linq to sql 速度性能怎么样?
csnd唐长老 2011-08-18
  • 打赏
  • 举报
回复
程序设计的有问题 你实现什么功能
jandhf 2011-08-18
  • 打赏
  • 举报
回复
值得学习。
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 bluedoctor 的回复:]
试试PDF.NET,可以这样做:

C# code


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年级");

……
[/Quote]

Linq2Sql 也可以嵌套SQL条件
bluedoctor 2011-08-18
  • 打赏
  • 举报
回复
试试PDF.NET,可以这样做:

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


拐点 2011-08-18
  • 打赏
  • 举报
回复
用Expression表达式目录树来构建你的查询条件
蛊惑精灵 2011-08-18
  • 打赏
  • 举报
回复
来学习的!
  • 打赏
  • 举报
回复

//方法和所在的类都必须为静态,以下只是简单演示,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));

不过最灵活的是用表达式树,不过上手要难一点
机器人 2011-08-18
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
比如你另外建一个静态类

写方法

//方法和所有类都必须为静态,以下只是简单演示,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);
}

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧