简体   繁体   English

如何在Linq中进行合并或分组? 我得到的倍数值想用','合并数据

[英]How to do concat or group by in linq? I am getting multiples values want to concat the data by ','

my Query: 我的查询:

var s = entities.Doctors.SingleOrDefault(x => x.AreaId == id);
                    var z = (from x in entities.Doctors
                             join y in entities.Areas on x.AreaId equals y.AreaId
                             join s1 in entities.Availabilties on s.D_Id equals s1.DoctorId
                             join s2 in entities.Eductions on s.D_Id equals s2.DoctorId
                             join s3 in entities.DoctorSpecialities on s.D_Id equals s3.DoctorId 
                             join s4 in entities.Specialities on s3.SpecialityId equals s4.SpecialityId
                             join s5 in entities.DaysDetails on s1.DaysId equals s5.DaysId
                             join s6 in entities.Degrees on s2.DegreeId equals s6.DegreeId
                             where x.AreaId.Equals(id)
                             select new DoctorDisplay
                             {
                                 D_name = x.D_Name,
                                 D_address = x.D_Address,
                                 D_Area = y.AreaName,
                                 D_Contact1 = x.D_Contactone, 
                                 D_Contact2 = x.D_Contacttwo,
                                 D_fax = x.D_Faxno,
                                 D_SpecialityName = s4.SpecialityName,
                                 D_Availstarttime = s1.StartTime,
                                 D_Availlasttime = s1.LastTime,
                                 D_Availday= s5.DaysName,
                                 D_DegreeName = s6.DegreeName,
                                 D_Awards = x.D_Address,
                                 D_Status = x.D_Status
                              }).ToList();

****Getting Output**** ****获得输出****

[{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"}]

Group_concat on the basis of Education Id, DoctorspecialitiesID, Avaibilities Id, Image of Input and desire Output 基于教育ID,DoctorspecialitiesID,职位ID, 输入图像和期望输出的 Group_concat

Looking at you example output, it looks less like you want to "concat the data by ','" as much as you want to convert it to JSON. 查看示例输出,就好像您想将其“转换为JSON一样”,而不是想要“用','合并数据”。

Inwhichcase, you'd probably be better off using a tool like Newtonsoft's JSON.NET 在这种情况下,最好使用Newtonsoft的JSON.NET之类的工具

UPDATE (based on comments): Assume doctors is the result of the query given in question. 更新(基于评论):假设doctors是有问题的查询的结果。

var output = 
    from d in doctors
     group d by new { d.D_NAME, d.D_Area } into dg
     select new DoctorDisplay
     {
        D_NAME = dg.Key.D_NAME,
        D_Area = dg.Key.D_Area,
        D_SpecialityName = String.Join(",", dg.Select(d => d.D_SpecialityName).Distinct()),
        D_DegreeName = String.Join(",", dg.Select(d => d.D_DegreeName).Distinct())
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM