How do I roll participants in benefit plans into two groups and get a count with LINQ to EF and C#
I have an interesting problem that I have been stumped by for a few days now. This problem requires s开发者_C百科ome background so please bear with me.
I am building a billing application for our benefits department. The data model consists of clients that have a 1:m relationship with plans. Plans, in turn, have a m:n relationship with employees. Plans need to be grouped into two groups for each client, FSA and HRA. Some types, such as DCA, FS1, etc. should be grouped as FSAs. Then all HRAs will be grouped together. I then need to get the count of employees that are a part of either the FSA group or HRA group. I am using Entity Framework and C#. Here is a workflow:
- Find Client(s)
- Find Client's current plans (plans where the start date < current date < end date)
- Group plans by FSA and HRA (DCA, FS1, etc. = FSA, HR1, HRA, HR2, etc = HRA)
- Find number of billable employees for each group
- return final distinct count of employees for each group (i.e. if one employee is part of DCA and FSA plan, only count them once under FSA)
Here is the SQL that I am using to get the numbers that I want for specific plans:
select COUNT(Distinct t1.Fis_Employee_Key)
from flex.FIS_EMPLOYEE_PLAN t1
LEFT OUTER JOIN (
Select Fis_Employee_Key, Fis_Emp_Can_Bill
from flex.FIS_EMPLOYEE) t2 ON t1.Fis_Employee_Key = t2.Fis_Employee_Key
where t1.Fis_Primehub_Plan_ID = 12345
OR t1.Fis_Primehub_Plan_ID = 12346
AND t2.Fis_Emp_Can_Bill = 'Y'
And here is the LINQ that I threw together in LINQPad to try to get plans grouped by Client's ID:
var clientPlans =
from c in FisClients
where !(c.Name.StartsWith("z-termed"))
&& (c.Plans.EndDate.CompareTo(currentDate) = 1)
group c.Plans by c.ID into planGroup
orderby planGroup.Key
select planGroup;
Then I was thinking of using clientPlans to then further group into FSA and HRA and return a distinct count but I can't figure out how.
Is this the right track? I would appreciate any help you could give. Thanks
精彩评论