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
foreach (CaseSelection CurrentCase in m_casesarraylist)
CurrentCaseList.Add(CurrentCase);
List
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);
AartiPosted Jan 4, 2012, 4:38 AM
to get the 3 controls that matches one case, for this see below code:
the SelectMany method to select all orders where TotalDue is less than 500.00.
decimal totalDue = 500.00M;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
ObjectSet
ObjectSet
var query =
contacts.SelectMany(
contact => orders.Where(order =>
(contact.ContactID == order.Contact.ContactID)
&& order.TotalDue < totalDue)
.Select(order => new
{
ContactID = contact.ContactID,
LastName = contact.LastName,
FirstName = contact.FirstName,
OrderID = order.SalesOrderID,
Total = order.TotalDue
}));
foreach (var smallOrder in query)
{
Console.WriteLine("Contact ID: {0} Name: {1}, {2} Order ID: {3} Total Due: ${4} ",
smallOrder.ContactID, smallOrder.LastName, smallOrder.FirstName,
smallOrder.OrderID, smallOrder.Total);
}
}
Jacques SandlerPosted Jan 4, 2012, 5:44 AM
I modified it a little bit, and there is the result which actually works if anyone is interested.
Many thanks
J
List CurrentCaseList = new List();
CurrentControlList = new List();
foreach (CaseSelection CurrentCase in m_casesarraylist)
CurrentCaseList.Add(CurrentCase);
List
foreach (ControlSelection CurrentControlRec in ControlList)
CurrentControlList.Add(CurrentControlRec);
var q2 =
CurrentCaseList.SelectMany(
c => CurrentControlList.Where(o => o.pracid == c.pracid && o.sex == c.sex &&
CaseSelectionList.AgeIsInRange(c.yob, o.yob, m_years))
.Select(o => new
{
CasePat = c.patid,
CasePrac = c.pracid,
ContPat = o.patid,
ContPrac = o.pracid
}).Take(m_ctrlno));
Jacques SandlerPosted Jan 4, 2012, 4:30 AM
e.g. if 'm_ctrlno' is equal to 2 (So 2 Controls to a Case) I get:
Case Patient 0001 Control Patient 0004
Case Patient 0001 Control Patient 0005
Which is correct, as the records fill the correct criteria. However it should do this for the whole Case list, not just one patient.
So i need to get the same as above for all the Case Patients and their Control matches.
Any ideas?