带有数组的动态where子句

时间:2011-05-07 19:19:56

标签: c# linq

假设我有一个带有int列表的对象作为其中一个属性:

public class MyObject
{
  public List<int> TheList
}

函数接收此对象作为Linq-To-SQL查询的参数,我有:

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

  }
}

基本上,参数包含一个int列表,我需要将这些int与l.Property匹配。你怎么写这种情况?

感谢您的建议。

1 个答案:

答案 0 :(得分:1)

where list.Contains(l.Property)

您可以执行多种条件:

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

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