I need to write it with a single query.
Depending on the parameter regionid must hold one of the conditions if the parameter is greater than zero regionid if this parameter is involved in the request if there is 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 good 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 ******p.region_id == myRegionId &&** gname.name.ToLower().Contains(filterText.ToLower())****
group p by new{ p.good_id} into 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 good 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 by new { p.good_id } into 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 about this?
if
myRegionId <= 0, then it evaluates totrueandp.region_id == myRegionIdis not evaluated and thus ignored.