I have a For each loop that is using linq to get info from the Visit table. It is nested with another For each loop that gets a name to list in the first column:
Caregiver | week1 | week2|....
__________|_______|______|....
John Smith| 2 | 3 |....
Mary Jones| 0 | 1 |....
It goes for 9 weeks and picks up a “Visit” count in the nested loop. I believe when I leave the inner loop it gives me a NULL error when it reaches the last Caregiver in the outer loop, and has an issue starting again from the top row for the next week (Week2).
I am getting an InvalidOperationException on the Select statement before I query the Visit table based on my Linq “visits”. What can I do to fix this?
EDIT: Here is an image of the error: https://i.stack.imgur.com/7oVtd.png
var phs = from cg in Context.CareGivers
join cgg in Context.CareGiverGroups on cg.car_gvr_int_id equals cgg.car_gvr_int_id
join o in Context.Organizations on cgg.org_int_id equals o.org_int_id
where cg.row_sta_cd.Trim() == "A"
&& cgg.alt_phy_id != null
&& cgg.alt_phy_id.Trim() != String.Empty
&& o.cli_acc_fg.Trim() == "Y"
&& o.org_int_id == 1468461
select cg;
int r = 1;
int s = 1;
for (int i = 0; i < 9; i++)
{
start = start.AddDays(i * -7);
end = start.AddDays(7);
foreach (var cg in phs)
{
// grab the correct exception, this will allow us to figure out where the issue might be
var visits = cg.CareGiverFunction.First(cgf => cgf.CodeDetail.cod_dtl_ds.Trim() == "Family Physician").VisitCareGiver.Select(vcg => vcg.Visit).AsQueryable();
visits = visits.Where(v => v.adm_ts >= start && v.adm_ts < end
&& (v.CodeDetail.cod_dtl_ext_id.Trim() == "I" || v.CodeDetail.cod_dtl_ext_id.Trim() == "V")
&& v.VisitStatusCdCodeDetail.cod_dtl_ext_id.Trim() != "CANCEL");
int counter = visits.Count();
String phys = cg.Person.DisplayName();
workbook.AddCell(r, 0, phys);
workbook.AddCell(r, s, counter);
r++;
}
s++;
}
cg.CareGiverFunction.First() might not be returning a result, this means that the rest of the code you have on that line doesn’t have any data to compare against.
Jon Skeet has answered a similar question, I’ll direct you to his post as I doubt I’ll be able to answer it any better than him: see here