views:

75

answers:

1
 var query_loc = (from at in db.amenities_types
                     join a in db.amenities on at.id equals a.amenities_type
                     join u in db.unitInfos on a.unit_id equals u.id
                     join l in db.locations on u.locations_id equals l.id
                     join o in db.organizations on l.organization_id equals o.id
                     join ot in db.organization_types on o.id equals ot.organization_id
                     where (((u.price >= low_rent) && (u.price <= high_rent)) 
                              || (u.price == null))
                     && (u.bedrooms <= beds) && (u.bathrooms <= baths)
                     && amenities_list.Contains(at.id)
                     && (((ot.active == true) && (DateTime.Now <= ot.deactivateDate))
                          || ((ot.active == true) && (ot.deactivateDate == null)))
                         && (((l.active == true) && (DateTime.Now <= l.deactivateDate))
                          || ((l.active == true) && (l.deactivateDate == null)) )
                     && (ot.type == 8)
                     orderby o.name ascending, l.name ascending
                     select new { l, o, u, ot, at });

The specific line I need to replace is

where amenities_list.Contains(at.id)

Instead it needs to produce SQL like this ([at.id] = 29 AND [at.id] = 30 AND [at.id] = 40)

So how do I get my List to produce the above SQL code in LINQ to SQL.

A: 

Please create methods for your clauses, you are scaring me.

var query_loc = (from at in db.amenities_types 
                 join a in db.amenities on at.id equals a.amenities_type 
                 join u in db.unitInfos on a.unit_id equals u.id 
                 join l in db.locations on u.locations_id equals l.id 
                 join o in db.organizations on l.organization_id equals o.id 
                 join ot in db.organization_types on o.id equals ot.organization_id 
                 where
                    PriceIsValid(u)
                 && BedsAndBathsArevalid(u) 
                 && AtIdIsValid(at.id)
                 && SomeCrazyDateConditionIsValid(ot, l)
                 && TheOtTypeIsValid(ot)
                 orderby o.name ascending, l.name ascending 
                 select new { l, o, u, ot, at }); 

And if you mean at.is = 29 OR at.id = 30 OR at.id = 40, then use an AtIdIsValid(at.id) predicate like:

bool AtIdIsValid(int atId){ return (atId == 29 || atId == 30 || atId == 40); }
Jimmy Hoffa
Thanks for the help people. I'm still used to writing SQL, this information has helped a great deal.
Caimen
It's linq-to-sql, this won't work. Linq-to-sql can't translate custom methods to SQL
jeroenh
jeroenh is right, you cant use custom functions in linq. OP did you get his code to work before marking as answer?
johnnywhoop
you are right after trying this out i wasn't able to actually get it to work. i ended up solving my problems with a left outer join. i went a different route.
Caimen