I have a linq subquery as follows:
for (int i = 0; i < parentAttributes.Length; i++)
{
Guid parent = parentAttributes[i];
var subQuery = from sc in db.tSearchCluendexes
join a in db.tAttributes on sc.AttributeGUID equals a.GUID
join pc in db.tPeopleCluendexes on a.GUID equals pc.AttributeGUID
where a.RelatedGUID == parent && userId == pc.CPSGUID
select sc.CPSGUID;
query = query.Where(x => subQuery.Contains(x.Id));
}
The basic idea is that I filter a list of data (as represented by the query variable) based on the results of a subquery executed an indeterminable number of times.
However I now have an issue whereby I only want to execute the where on the query if the subquery actually contains data and I don't want to execute the subquery within each iteration of the loop (for example by using a Count() or a ToList()) as this will hit the remote resource. Something like below if we take it that a method HasData() actually returns true or false:
for (int i = 0; i < parentAttributes.Length; i++)
{
Guid parent = parentAttributes[i];
var subQuery = from sc in db.tSearchCluendexes
join a in db.tAttributes on sc.AttributeGUID equals a.GUID
join pc in db.tPeopleCluendexes on a.GUID equals pc.AttributeGUID
where a.RelatedGUID == parent && userId == pc.CPSGUID
select sc.CPSGUID;
if (subQuery.HasData())
query = query.Where(x => subQuery.Contains(x.Id));
}
Suggestions are welcome.