Как создать динамическое предложение where с помощью List в LINQ to SQL? - PullRequest
0 голосов
/ 31 августа 2010
 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 });

Конкретная строка, которую мне нужно заменить, -

, где esta_list.Contains (at.id)

Вместо этого необходимо создать SQL-код, подобный этому ([at.id] =29 AND [at.id] = 30 AND [at.id] = 40)

Итак, как мне получить свой список для создания приведенного выше кода SQL в LINQ to SQL.

1 Ответ

0 голосов
/ 31 августа 2010

Пожалуйста, создайте методы для ваших предложений, вы меня пугаете.

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 }); 

И если вы имеете в виду at.is = 29 ИЛИ at.id = 30 ИЛИ at.id = 40, то используйте AtIdIsValid(at.id) предикат вроде:

bool AtIdIsValid(int atId){ return (atId == 29 || atId == 30 || atId == 40); }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...