Search code examples
linqdb4o

Conditional clauses for linq to Db4O query?


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

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

This results in

  1. a complete enumeration of ALL the colors
  2. a filter by name.

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


Solution

  • Would something like this be suitable?

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