Solved

LINQ, WHERE clause issue

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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