Solved

LINQ, WHERE clause issue

Posted on 2014-12-03
3
223 Views
Last Modified: 2016-02-15
Question: Why lines 5 and 6 giving me trouble?

How can I fix this. I have used this sort of syntax for years in SQL work. What is the equivalent in LINQ (vb.net)?
            Dim ODs = (From od In db.tblOrderDetails Join o In _
                               db.tblOrders On od.OrderID Equals o.OrderID Join c In _
                               db.tblCustomers On o.CustomerID Equals c.CustomerID Join s In _
                               db.tblSoftwares On od.SoftwareID Equals s.SoftwareID _
                               Where o.OrderID = IIf(intOrderID = 0, o.OrderID, intOrderID) And _
                                o.CustomerID = IIf(intCustID = 0, o.CustomerID, intCustID) _
                               Order By c.FirstName & " " & c.LastName _
                               Select CustName = c.FirstName & " " & c.LastName, _
                                      od.OrderID, o.OrderDate, s.Title, od.Quantity, s.UnitPrice, Sub_Total = od.Quantity * s.UnitPrice).ToArray()

Open in new window


Please see: http://developwith.net/2012/07/20/where-if-and-where-iif-linq/ if it could help. I may need some help in its implementation.
0
Comment
Question by:Mike Eghtebas
3 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40480267
I would have written your where clause as
Where (intOrderID = 0 OrElse o.OrderID = intOrderID) And _
      (intCustID = 0 OrElse o.CustomerID = intCustID)

Open in new window

but your code should do the job.

If you want to use the WhereIf extension method, you must use function call syntax, not LINQ syntax
Dim ODs = db.tblOrderDetails _
    .Join(db.tblOrders, Function(od) od.OrderID, Function(o) o.OrderID, Function(od, o) New With {od, o}) _
    .Join(db.tblCustomers, Function(j) j.o.CustomerID, Function(c) c.CustomerID, Function(j, c) New With {j.o, j.od, c}) _
    .Join(db.tblSoftwares, Function(j) j.od.SoftwareID, Function(s) s.SoftwareID, Function(j, s) New With {j.o, j.od, j.c, s}) _
    .AsQueryable() _
    .WhereIf(intOrderID <> 0, Function(j) j.o.OrderID = intOrderID) _
    .WhereIf(intCustID <> 0, Function(j) j.o.CustomerID = intCustID) _
    .OrderBy(Function(j) j.c.FirstName & " " & j.c.LastName) _
    .Select(Function(j) New With {.CustName = j.c.FirstName & " " & j.c.LastName, _
            j.od.OrderID, j.o.OrderDate, j.s.Title, j.od.Quantity, j.s.UnitPrice, .Sub_Total = j.od.Quantity * j.s.UnitPrice}).ToArray()

Open in new window

or mix the two syntaxes:
Dim ODs = (From j In (From od In db.tblOrderDetails Join o In _
            db.tblOrders On od.OrderID Equals o.OrderID Join c In _
            db.tblCustomers On o.CustomerID Equals c.CustomerID Join s In _
            db.tblSoftwares On od.SoftwareID Equals s.SoftwareID) _
            .AsQueryable() _
            .WhereIf(intOrderID <> 0, Function(j) j.o.OrderID = intOrderID) _
            .WhereIf(intCustID <> 0, Function(j) j.o.CustomerID = intCustID) _
            Order By c.FirstName & " " & c.LastName _
            Select CustName = c.FirstName & " " & c.LastName, _
                    od.OrderID, o.OrderDate, s.Title, od.Quantity, s.UnitPrice, Sub_Total = od.Quantity * s.UnitPrice).ToArray()

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40480731
What is the error?
0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 40490632
kaufmed,

My apology for not responding earlier. It didn't like IIF() syntax. I do not recall exact description. I have revised the solution since. But later I will try the solution from louisfr which seems a working solution.

Thanks,

Mike
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question