Solved

LINQ, WHERE clause issue

Posted on 2014-12-03
3
215 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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

14 Experts available now in Live!

Get 1:1 Help Now