linq long where clause

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?

thanks!
DATA99Asked:
Who is Participating?
 
Easwaran ParamasivamCommented:
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.
0
 
DATA99Author Commented:
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.
0
 
DATA99Author Commented:
did the trick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.