query = from bp in query
join sm in _storeMappingRepository.Table
on new { c1 = bp.Id, c2 = "BlogPost" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into bp_sm
from sm in bp_sm.DefaultIfEmpty()
where !bp.LimitedToStores || storeId == sm.StoreId
join sm in _storeMappingRepository.Table
on new { c1 = bp.Id, c2 = "BlogPost" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into bp_sm
from sm in bp_sm.DefaultIfEmpty()
where !bp.LimitedToStores || storeId == sm.StoreId
select bp;
query = from bp in query
group bp by bp.Id
into bpGroup
orderby bpGroup.Key
select bpGroup.FirstOrDefault();