frenchie
frenchie

Reputation: 51927

dynamic where clause with array

Let's say I have an object with a list of int as one of the properties:

public class MyObject
{
  public List<int> TheList
}

A function receives this object as a parameter for a Linq-To-SQL query and I have this:

public static List<MyModel> ConditionalQuery(MyObject TheObject)
{   
  using (MyDataContext TheDC = new MyDataContext())
  {
     var TheListBuilder = (from l in TheDC

                           where l.Property = the elements in the list

                           select l.ID).ToList();

      return new List<MyModel>(TheListBuilder);

  }
}

Basically, the parameter contains a list of ints and I need to match these ints with the l.Property. How do you write this type of condition?

Thanks for your suggestions.

Upvotes: 1

Views: 735

Answers (1)

Alex Aza
Alex Aza

Reputation: 78447

where list.Contains(l.Property)

for multiple conditions you can do:

        var result = (
            from l in TheDC.Table
            where list1.Contains(l.Property1)
            where list2.Contains(l.Property2)
            select l.Id
            ).ToList();

or

        var result = (
            from l in TheDC.Table
            where list1.Contains(l.Property1) && list2.Contains(l.Property2)
            select l.Id
            ).ToList();

Upvotes: 1

Related Questions