3
Answers

C# Linq Select Many

Jacques Sandler

Jacques Sandler

12y
2.8k
1
Hi All,

I am matching Cases to Controls, basically records in the Case list, need to have the number of matches that is specified in the string 'm_ctrlno'.

So far i have two lists, the where clause is correct, however i'm unsure how to use SelectMany to get the 3 Controls that match 1 Case. I decided to use the .Take() function however it doesn't seem to be working. I'm not getting the same case with 3 different controls when i cycle on the var query.

Any ideas?

Many thanks
J.


Here is the code:

List<CaseSelection> CurrentCaseList = new List<CaseSelection>();
foreach (CaseSelection CurrentCase in m_casesarraylist)
CurrentCaseList.Add(CurrentCase);

List<ControlSelection> CurrentControlList = new List<ControlSelection>();
foreach (ControlSelection CurrentControlRec in ControlList)
CurrentControlList.Add(CurrentControlRec);


var query = CurrentCaseList.SelectMany(
c => CurrentControlList.Where(o => o.pracid == c.pracid && o.sex == c.sex &&
CaseSelectionList.AgeIsInRange(c.yob, o.yob, m_years)),
(c, o) =>
new { o, c }).Take(m_ctrlno);

Answers (3)