title: C# Dynamic Linq Query Multiple OR Logic
body: <p>I'm not a C# programmer, nor did I design the database for this, but I've been tasked with creating a Linq query that doesn't know what columns to use at compile time. Trying to be succinct, here's the gist of what I'm trying to do:</p>
  
  <p>From the frontend, the user can select one or more groups and expect back a JSON string containing those users that will fill up a paginated table.</p>
  
  <p>Let's say I have these groups:</p>
  
  <p>Group1</p>
  
  <p>Group2</p>
  
  <p>Group3</p>
  
  <p>Group4</p>
  
  <p>and the user selects Group1, Group2, and Group4. They expect back from the database users that belong to any one of these groups.</p>
  
  <p>In the database, these group names are column names.</p>
  
  <p>If I were to write a straight SQL statement it would look something like this:</p>
  
  <pre><code>SELECT EmailAddress, FirstName, LastName
  FROM Contacts AS C
  JOIN GroupContacts AS D
  ON C.ID = D.ContactId
  WHERE D.DealId = 'some unique id'
  AND (
          D.Group1 = 1
          OR
          D.Group2 = 1
          OR
          D.Group4 = 1
      )
  ORDER BY C.LastName
  OFFSET 0 ROWS
  FETCH NEXT 10 ROWS ONLY
  </code></pre>
  
  <p>Where the  = 1 means that Contact is in that group. </p>
  
  <p>All I want to be able to do is to make that AND (...) part dynamic, depending on what the user has selected. </p>
  
  <p>I've investigated how to do it with C# Expressions, but the functions I'm coming across (Expression.OrElse()) can only take two parameters. </p>
  
  <p>What is my approach here?</p>
  
  <p>EDIT:</p>
  
  <p>Here is my sad C# code:</p>
  
  <pre><code>String[] GroupNames = { "Group1", "Group2", "Group4" }; // user selected groups
  List&lt;Expression&gt; expressionList = new List&lt;Expression&gt;();
  
  foreach (String name in GroupNames)
  {
      expressionList.Add(Expression.Equal(Expression.Constant(name), 1));
  }
  </code></pre>
  
  <p>Here is where I'm really not sure what to do. I think I could write the rest of the expression if I could just get this expressionList to evaluate. </p>
  
  <p>I know that I will have a finite number of groups. At first I tried writing a switch statement sort of solution:</p>
  
  <pre><code>List&lt;Expression&gt; expressionList = new List&lt;Expression&gt;();
  Expression expressionOR = Expression.Empty();
  Expression rightSide = Expression.Constant(true, typeof(bool));
  
  foreach (string name in GroupNames)
  {
      switch (name)
      {
          case "Group1":
              leftSide = Expression.Constant("Group1");
              e1 = Expression.Equal(leftSide, rightSide);
              break;
  
          case "Group2":
              leftSide = Expression.Constant("Group2");
              e2 = Expression.Equal(leftSide, rightSide);
              break;
  
          case "Group3":
              leftSide = Expression.Constant("Group3");
              e3 = Expression.Equal(leftSide, rightSide);
              break;
  
          case "Group4":
              leftSide = Expression.Constant("Group4");
              e4 = Expression.Equal(leftSide, rightSide);
              break;
      }
  
      expressionOR = Expression.Equal(leftSide, rightSide);
      expressionList.Add(expressionOR);
  }
  </code></pre>
  
  <p>And then I would try to use Expression.OrElse(expressionList); is some way. </p>
  
  <p>Forgive my naivety on C#, and thanks so far to everybody who has responded. It's really appreciated.  </p>