与Linq集团连接

时间:2018-03-28 18:39:09

标签: linq

我有这个:

var lstAssignmentDetails =  
     (from t1 in _unitOfWork.ReferralDetailsRepository.Get()
    .Where(m => (m.ChartStatusID == (int)Utility.ReferralChartStatus.NotStaffed 
         || m.ChartStatusID == (int)Utility.ReferralChartStatus.Partially_Staffed 
         || m.ChartStatusID == (int)Utility.ReferralChartStatus.Restaff) 
         && m.IsDeleted == false).OrderByDescending(x => x.ReferralDetailsId)  
    join t2 in _unitOfWork.ReferralRepository.Get() on t1.ReferralId equals t2.ReferralId
    join t3 in _unitOfWork.PatientRepository.Get() on t2.PatientId equals t3.PatientId
    join t4 in _unitOfWork.ClientRepository.Get() on t2.ClientId equals t4.ClientID
    join t5 in _unitOfWork.DisciplineRepository.Get() on t1.DesciplineID equals t5.DesciplineID
    join t6 in _unitOfWork.UserRepository.Get() on t2.CreatedBy equals t6.UserID
    join t7 in _unitOfWork.PersonRepository.Get() on t6.PersonID equals t7.PersonID
    join rt in _unitOfWork.ReferralTherapistRepository.Get() on t2.ReferralId equals rt.ReferralId
    join t in _unitOfWork.TherapistRepository.Get() on rt.TherapistId equals t.TherapistId 
    join u in _unitOfWork.UserRepository.Get() on t.UserId equals u.UserID
    join p in _unitOfWork.PersonRepository.Get() on u.PersonID equals p.PersonID

select new ReferralTempModel()
{
    ReferralId = t1.ReferralId,
    ClientName = t4.ClientName,
    PatientName = t3.LastName + "," + t3.FirstName,
    RefferalDate = t2.RefferalDate,
    DisciplineID = t1.DisciplineID,
    ReferralDetailsId = t1.ReferralDetailsId,
    PatientId = t2.PatientId,
    ClientID = t4.ClientID,
    DiscName = t5.DesciplineType,
    IsRejected = t1.IsRejected,
    CreatedBy = t7.LastName + "," + t7.FirstName,
    ChartstatusId = t1.ChartStatusID.Value
}).ToList();

return lstAssignmentDetails;

每个推荐都有多个推荐治疗师。我需要将每个推荐的所有推荐治疗师连接到一个字段中。基本上我想要做的是使用LINQ的GROUP_CONCAT。

我知道这可能是重复的,但以前的问题都没有处理像这样复杂的事情。尝试添加GroupBy似乎总是会产生错误,无论我放在哪里。

1 个答案:

答案 0 :(得分:1)

在构建单位列表时,以 加入其他推荐作为t2开始,并将匿名类型的治疗师名称添加:

var flatListAssignmentDetails =  
     (from t1 in _unitOfWork.ReferralDetailsRepository.Get()
    .Where(m => (m.ChartStatusID == (int)Utility.ReferralChartStatus.NotStaffed 
         || m.ChartStatusID == (int)Utility.ReferralChartStatus.Partially_Staffed 
         || m.ChartStatusID == (int)Utility.ReferralChartStatus.Restaff) 
         && !m.IsDeleted)
    join t2 in _unitOfWork.ReferralRepository.Get() on t1.ReferralId equals t2.ReferralId
    join t3 in _unitOfWork.PatientRepository.Get() on t2.PatientId equals t3.PatientId
    join t4 in _unitOfWork.ClientRepository.Get() on t2.ClientId equals t4.ClientID
    join t5 in _unitOfWork.DisciplineRepository.Get() on t1.DesciplineID equals t5.DesciplineID
    join t6 in _unitOfWork.UserRepository.Get() on t2.CreatedBy equals t6.UserID
    join t7 in _unitOfWork.PersonRepository.Get() on t6.PersonID equals t7.PersonID
select new {
    t1.ReferralId,
    t4.ClientName,
    PatientName = t3.LastName + "," + t3.FirstName,
    t2.RefferalDate,
    t1.DisciplineID,
    t1.ReferralDetailsId,
    t2.PatientId,
    t4.ClientID,
    DiscName = t5.DesciplineType,
    t1.IsRejected,
    CreatedBy = t7.LastName + "," + t7.FirstName,
    ChartstatusId = t1.ChartStatusID.Value,
    TherapistName = p.LastName + "," + p.FirstName
}).ToList();

现在,您可以对列表进行分组,并连接治疗师姓名:

var listAssignmentDetails = flatListAssignmentDetails
    .GroupBy(r => r.ReferralId)
    .Select(g => new ReferralTempModel {
        ReferralId = g.First().ReferralId,
        ClientName = g.First().ClientName,
        PatientName = g.First().PatientName,
        RefferalDate = g.First().RefferalDate,
        DisciplineID = g.First().DisciplineID,
        ReferralDetailsId = g.First().ReferralDetailsId,
        PatientId = g.First().PatientId,
        ClientID = g.First().ClientID,
        DiscName = g.First().DiscName,
        IsRejected = g.First()..IsRejected,
        CreatedBy = g.First().CreatedBy,
        ChartstatusId = g.First().ChartStatusID,
        TherapistNames = string.Join(", ", g.Select(r => r.TherapistName))
    }).ToList();

这里需要注意的是,与组中所有引荐相关联的所有字段都是通过g.First()构造获得的。具有"组串联的字段"使用string.Join方法生成TherapistName投影组。