Solved

LINQ, WHERE clause issue

Posted on 2014-12-03
3
219 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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now