-1

Let's say I have a list with 2 or more customerIds and a list with two or more order dates. I want an SQL query like this from linq to sql

SELECT * 
FROM Orders 
WHERE (CustomerId = @CustomerId1 
       AND (OrderDate = @OrderDate1 OR OrderDate = @OrderDate2)) 
      OR
      (CustomerId = @CustomerId2 
       AND (OrderDate = @OrderDate1 OR OrderDate = @OrderDate2)) 

The list with CustomerIds and order dates is not fixed, so I need to loop through it when building the query.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mr Zach
  • 495
  • 4
  • 18
  • You will have to add much more code and context if you expect any actual help – maccettura Sep 14 '17 at 21:53
  • That would likely be better as `SELECT * FROM Orders where CustomerId IN (@CustomerId1, @CustomerId2) AND OrderDate IN (@OrderDate1 , @OrderDate2)` - but yes please show your existing C# code. – mjwills Sep 14 '17 at 21:56
  • This is just an example to illustrate what I need. What I need is a way to generate OR and AND conditions from multiple lists/arrays with information. It cannot be made into something like your example, like this link. from Order in Orders where ListWithOrders.Containts(Order.OrderId) && (Order.OrderDate == OrderDate1 || Order.OrderDate == OrderDate2).. – Mr Zach Sep 14 '17 at 22:02
  • Yes there is. If you can show us your existing LINQ to SQL code we can give you some pointers. – mjwills Sep 14 '17 at 23:06

1 Answers1

0

I found a solution for this by using PredicateBuilder from http://www.albahari.com/nutshell/predicatebuilder.aspx

Mr Zach
  • 495
  • 4
  • 18