1
join
및 where
절을 여러 쿼리에 복제 할 필요가 없도록이 코드를 다시 작성할 수있는 방법이 있습니까?LINQ 조건부 합계
if (categoryId > 0)
{
query = from p in _productRepository.Table
from pv in p.ProductVariants.DefaultIfEmpty()
from pc in p.ProductCategories.Where(pc => pc.CategoryId == categoryId)
join psa in _productSpecificationAttributeRepository.Table on p.Id equals psa.ProductId
join sao in _specificationAttributeOptionRepository.Table on psa.SpecificationAttributeOptionId equals sao.Id
join sa in _specificationAttributeRepository.Table on sao.SpecificationAttributeId equals sa.Id
where p.Published && pv.Published && !p.Deleted && psa.AllowFiltering &&
(!pv.AvailableStartDateTimeUtc.HasValue || pv.AvailableStartDateTimeUtc.Value < nowUtc) &&
(!pv.AvailableEndDateTimeUtc.HasValue || pv.AvailableEndDateTimeUtc.Value > nowUtc)
select new {sa, sao};
}
else if (brandId > 0)
{
query = from p in _productRepository.Table
from pv in p.ProductVariants.DefaultIfEmpty()
from pb in p.ProductBrands.Where(pb => pb.BrandId == brandId)
join psa in _productSpecificationAttributeRepository.Table on p.Id equals psa.ProductId
join sao in _specificationAttributeOptionRepository.Table on psa.SpecificationAttributeOptionId equals sao.Id
join sa in _specificationAttributeRepository.Table on sao.SpecificationAttributeId equals sa.Id
where p.Published && pv.Published && !p.Deleted && psa.AllowFiltering &&
(!pv.AvailableStartDateTimeUtc.HasValue || pv.AvailableStartDateTimeUtc.Value < nowUtc) &&
(!pv.AvailableEndDateTimeUtc.HasValue || pv.AvailableEndDateTimeUtc.Value > nowUtc)
select new {sa, sao};
}
//only distinct attributes (group by ID)
query = from x in query
group x by x.sao.Id into xGroup
orderby xGroup.Key
select xGroup.FirstOrDefault();
2 개 쿼리 사이에 변경 유일한 것은이 조인 :와
from pc in p.ProductCategories.Where(pc => pc.CategoryId == categoryId)
이 가입 : 당신은이 where 절에 가입 이동 한 경우
from pb in p.ProductBrands.Where(pb => pb.BrandId == brandId)
나는이 라인 때문에 일 것입니다 생각하지 않습니다 'pc에서 p.ProductCategories.Where (pc => pc.CategoryId == categoryId)'는 SQL에서 CROSS JOIN을 생성하지만 응답의 where 절은 SQL에서 생성하지 않습니다. –