0

Example object

public class Car
{
     public int Doors { get; set; }
     public int Wheels { get; set; }
}

To find all cars that match a list:

var list = new[] { 3, 4 };
var result = Db.Cars.Where( c => list.Contains(c.Doors) );

How to find all the cars that match one of the combinations of doors and wheels? I've tried:

var combinations = new[] { new Comb { Doors = 1, Wheels = 2 }, new Comb { doors = 5, wheels = 4 } };
var result = Db.Cars.Where( c => combinations.Contains(new Comb { Doors = c.Doors, Wheels = c.Wheels }) ) );

But of course this doesn't work, giving the following error:

Unable to create a constant value of type 'Example.Comb'. Only primitive types or enumeration types are supported in this context.

Is there another way to do this?

Important part is that the amount of combinations is unknown.

Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
  • 1
    Your `Comb` object looks just like a `Car` object. What if you create a list of `Car` objects instead? – juharr Oct 16 '15 at 19:29
  • Hi Juharr, great tip and I tried it out. I'm affraid it gives me the same error for LinqToSql: *Unable to create a constant value of type 'Example.Car'. Only primitive types or enumeration types are supported in this context.* – Dirk Boer Oct 16 '15 at 19:43

1 Answers1

0
var result = Db.Cars.Where(c=>combinations.Any(comb=>comb.Doors==c.Doors && comb.Wheels==c.Wheels));
Robert McKee
  • 21,305
  • 1
  • 43
  • 57