linq long where clause

Posted on 2012-09-02
Last Modified: 2012-09-04
Hi i have a long where clause:
var Properties = from m in db.PropertySales
                                join im in db.Images on m.ID equals im.PropertySaleRef
                                where m.AgentRef.ToString() == "48" && m.StatusRef.ToString() == "1" && m.Archived.ToString() == "False" && im.Selected.ToString() == "True" && m.Bedrooms >= bedrooms && m.Price >= minprice2 && m.Price <= maxprice2 
                                select m;

Open in new window

now i want to add this:

(area != null && areaIds.Contains(m.SubAreaRef))
&& (type != null && typeIds.Contains(m.SubTypeRef))
&& (subarea != null && m.SubAreaRef == subarea)
&& (subtype != null && m.SubTypeRef == subtype)

but i want to do in in a way that it only checks for area, when subarea = null, same with type/subtype

also when area/subarea = null, it should still do the checks for type/subtype and the otherway around..

what is the best way to wright this query?

Question by:DATA99
    LVL 16

    Accepted Solution

    You could use Conditional operator for it. Redefine your conditions as follows:

    (( subarea  == null ) ?  ((area != null && areaIds.Contains(m.SubAreaRef) ? true : false)  : (m.SubAreaRef == subarea) ? true :  false))  &&  (( subtype == null ) ?  (((type != null && typeIds.Contains(m.SubTypeRef) ? true : false)  : (m.SubTypeRef == subtype) ? true :  false))

    Open in new window

    For more information look at

    I hope this helps.

    Author Comment

    don't want to get true or false back though.... it should only select where m.SubAreaRef = subarea,    if subarea != null.

    with area it should only do areaIds.Contains(m.SubAreaRef) when area != null, and subarea != null.....   same for the types, and of course the original query.

    Author Closing Comment

    did the trick

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    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.

    745 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

    19 Experts available now in Live!

    Get 1:1 Help Now