Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


linq long where clause

Posted on 2012-09-02
Medium Priority
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
  • 2
LVL 16

Accepted Solution

Easwaran Paramasivam earned 2000 total points
ID: 38359934
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 http://blog.newslacker.net/2008/07/c-conditional-shorthand.html

I hope this helps.

Author Comment

ID: 38361443
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

ID: 38362785
did the trick

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

580 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