LINQ to Db4O查询的条件子句? [英] Conditional clauses for linq to Db4O query?

查看:92
本文介绍了LINQ to Db4O查询的条件子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在linq to sql中,我可以这样:

In linq to sql i can do like this:

var q = db.Colors;
if(! string.IsNullOrEmpty(colorName))
   q = q.Where(c=>c.Name.Equals(colorName));
return q.ToList();

在Db4O linq中,我无法做到这一点,因为我必须先入手

In Db4O linq I can't do it like this because I have to start with

var q = (from Color c in db
         select c);
if(! string.IsNullOrEmpty(colorName))
   q = q.Where(c=>c.Name.Equals(colorName));
return q.ToList();

这将导致

  1. 所有颜色的完整枚举
  2. 按名称过滤.

这不是我旨在偏离目标的解决方案. 有什么建议吗?

That's not the solution I was aiming for off course. Any suggestions?

推荐答案

像这样的东西适合吗?

return (from Color c in db
       where !String.IsNullOrEmpty(colorName) && c.Name.Equals(colorName)
       select c).ToList();

然后,您还可以使用多个参数:

You can then also use multiple parameters:

return (from Color c in db
       where (!String.IsNullOrEmpty(colorName) && c.Name.Equals(colorName))
          || (!String.IsNullOrEmpty(color1Name) && c.Name.Equals(color1Name))
          || (!String.IsNullOrEmpty(color2Name) && c.Name.Equals(color2Name))
          ...
       select c).ToList();

这篇关于LINQ to Db4O查询的条件子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆