?
Solved

LINQ, WHERE clause issue

Posted on 2014-12-03
3
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 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 34

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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