I’m trying to translate a linq query using query keywords to the method syntax.
My ultimate goal is to be able to apply additonnal restrictions at runtime each time you see a Where clause in the query. I have a total of 14 different parameters to be applied. Some are mutually exclusive, other are inclusive.
Here is the starting query :
query = from p in context.Person
where p.Firstname.ToUpper().StartsWith(firstName.ToUpper())
join v in context.Visit on p.Id equals v.PersonId
where !v.AccessionNumber.StartsWith(RISConst.PM_PREFIX)
join f in context.Finding on v.Id equals f.VisitId
join c in context.Consultation on f.Id equals c.FindingId
where c.StudyId.ToUpper().CompareTo(studyId.ToUpper()) == 0
select v;
Using Jon Skeet’s excellent blog post I have been able to go that far :
query = context.Person.Where(p => p.Firstname.ToUpper().StartsWith(firstName.ToUpper()))
.Join(context.Visit, p => p.Id, v => v.Id, (p, v) => new { p, v })
.Where(z => z.v.AccessionNumber.StartsWith(RISConst.PM_PREFIX))
.Select(z => z.v)
.Join(context.Finding, v => v.Id, f => f.VisitId, (v, f) => new { v, f })
.Select(t => t.f)
.Join(context.Consultation, f => f.Id, c => c.FindingId, (f, c) => new { f, c })
.Where( u => u.c.StudyId.ToUpper().CompareTo(studyId.ToUpper()) == 0)
.Select(????);
Now I’m stuck because I need to return Visits. How can I do that ?
Any Help appreciated.
I think you lost
Visistson this select statement:.Select(t => t.f). Remove it and work with whole anonymous object on next statement.