Conditional sentences for query linq to Db4O?

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 cannot do it like this because I need 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();

The result is

  • full listing of all colors
  • filter by name.

This is not the solution I was striving for. Any suggestions?

0
source share
3 answers

Could this be appropriate?

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

Then you can also use several 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();
+2
source

I'm not sure what you're driving at. You are worried that in the first case, part of the code is executed by the server side, so you optimize the return values. But in the second case, the enumeration is performed locally, so there is no optimization according to the values ​​used?

, LINQ to Objects. , , .

+1

, :

IDb4oLinqQuery<Color> q;
if(! string.IsNullOrEmpty(colorName))
{
  q = from Color c in db
      where c.Name.Equals(colorName)
      select c;
}
else
{
  q = from Color c in db
      select c;
}
return q.ToList();

So the Db4O preprocessor sees 2 different LINQ queries? Downside is off, this solution is much more detailed and not quite harsh.

0
source

Source: https://habr.com/ru/post/1724390/


All Articles