I have the following query:
from p in dataContext.Repository<IPerson>() join spp1 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp1.PersonId join s1 in dataContext.Repository<ISports>() on spp1.SportsId equals s1.Id join spp2 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp2.PersonId join s2 in dataContext.Repository<ISports>() on spp2.SportsId equals s2.Id where s1.Name == "Soccer" && s2.Name == "Tennis" select new { p.Id };
He chooses everyone who plays soccer and tennis.
At run time, the user can select other tags to add to the request, for example: "Hockey". Now my question is: how can I dynamically add Hockey to the request? If Hockey is added to the request, it will look like this:
from p in dataContext.Repository<IPerson>() join spp1 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp1.PersonId join s1 in dataContext.Repository<ISports>() on spp1.SportsId equals s1.Id join spp2 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp2.PersonId join s2 in dataContext.Repository<ISports>() on spp2.SportsId equals s2.Id join spp3 in dataContext.Repository<ISportsPerPerson>() on p.Id equals spp3.PersonId join s3 in dataContext.Repository<ISports>() on spp3.SportsId equals s3.Id where s1.Name == "Soccer" && s2.Name == "Tennis" && s3.Name == "Hockey" select new { p.Id };
It would be preferable if the query would dynamically grow like this:
private void queryTagBuilder(List<string> tags) { IDataContext dataContext = new LinqToSqlContext(new L2S.DataContext()); foreach(string tag in tags) {
Does anyone have an idea how to properly set this up? Thanks in advance!
source share