Entity Framework Core using select and group by

asp.net-core c# entity-framework entity-framework-core

Question

I want to use select and groupby in my query

_context.UserChart_Tbl
        .Where(uc => uc.UserID == "6")
        .GroupBy(g => g.OrgnizationID)
        .Select(g => g.OrgnizationID)
        .ToArray();

But I get an error in select

Error CS1061 'IGrouping' does not contain a definition for 'OrgnizationID' and no accessible extension method 'OrgnizationID' accepting a first argument of type 'IGrouping' could be found (are you missing a using directive or an assembly reference?)

How can I solve this problem?

Update

Error Solved by this code

_context.UserChart_Tbl
          .Where(uc => uc.UserID == u.Id)
          .GroupBy(g => g.OrgnizationID)
          .Select(g => g.Key).ToArray()

Complete Code

    public List<DoctorDropDoenViewModel> DoctorDropDown()
    {
        var query = (from u in _context.Users
                     join
                     UR in _context.UserRoles on u.Id equals UR.UserId
                     join
                     D in _context.Doctor_Tbl on u.Id equals D.DoctorUserId
                     where UR.PolicyID == 4

                     select new DoctorDropDoenViewModel()
                     {
                         DoctorID = u.Id,
                         DoctorFullName = u.FirstName + " " + u.Family,
                         OrgID = _context.UserChart_Tbl.Where(uc => uc.UserID == u.Id)
                         .GroupBy(g => g.OrgnizationID).Select(g => g.Key).ToArray()
                     });


      return query.ToList();
    }

And

public class DoctorDropDoenViewModel
{
    public string DoctorID { get; set; }
    public string DoctorFullName { get; set; }
    public int[] OrgID { get; set; }
}

DoctorID can have several OrgID Which may also be duplicates. I want to display duplicates only once. For this purpose i use Distinct and Group By.

But Group By not working

enter image description here

1
2
2/1/2020 1:20:06 PM

Popular Answer

You can use the following code

_context.UserChart_Tbl.Where(uc => uc.UserID == u.Id)
.Where(x=>x.DoctorID==1).Select(g => g.OrgnizationID).Distinct().ToArray()}
0
2/1/2020 1:32:22 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow