c# - How can I dynamically change the condition of where, depending on the parameter -
i need write single query. depending on parameter regionid must hold 1 of conditions if parameter greater 0 regionid if parameter involved in request if there not involved.
var myregionid = 0; if (!string.isnullorempty(regionid)) { myregionid = int.parse(regionid); } iorderedqueryable price; if (myregionid>0) { price = (from p in _db.prices join in _db.goods on p.good_id equals good.id join gname in _db.spr_goods_names on good.goods_name_id equals gname.id ******p.region_id == myregionid &&** gname.name.tolower().contains(filtertext.tolower())**** group p new{ p.good_id} g select new { goodid = g.key.good_id, promotion = g.count(x => x.promotion != ""), minprice = g.min(x => x.good_price), distributorcount = g.count(x => x.distributor_id != null) } ).orderbydescending(x => x.distributorcount).take(100).orderby(x => x.minprice); }else { price = (from p in _db.prices join in _db.goods on p.good_id equals good.id join gname in _db.spr_goods_names on good.goods_name_id equals gname.id **where gname.name.tolower().contains(filtertext.tolower())** group p new { p.good_id } g select new { goodid = g.key.good_id, promotion = g.count(x => x.promotion != ""), minprice = g.min(x => x.good_price), distributorcount = g.count(x => x.distributor_id != null) } ).orderbydescending(x => x.distributorcount).take(100).orderby(x => x.minprice); }
how this?
price = (from p in _db.prices join in _db.goods on p.good_id equals good.id join gname in _db.spr_goods_names on good.goods_name_id equals gname.id (myregionid <= 0 || p.region_id == myregionid) && gname.name.tolower().contains(filtertext.tolower()) group p new{ p.good_id} g select new { goodid = g.key.good_id, promotion = g.count(x => x.promotion != ""), minprice = g.min(x => x.good_price), distributorcount = g.count(x => x.distributor_id != null) }).orderbydescending(x => x.distributorcount).take(100).orderby(x => x.minprice); if myregionid <= 0, evaluates true , p.region_id == myregionid not evaluated , ignored.
Comments
Post a Comment