简体   繁体   English

Entity Framework Core 使用选择和分组依据

[英]Entity Framework Core using select and group by

I want to use select and groupby in my query我想在查询中使用selectgroupby

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

But I get an error in select但我在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?)错误 CS1061“IGrouping”不包含“OrgnizationID”的定义,并且找不到接受“IGrouping”类型的第一个参数的可访问扩展方法“OrgnizationID”(您是否缺少 using 指令或程序集引用?)

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. DoctorID可以有多个OrgID也可能是重复的。 I want to display duplicates only once.我只想显示重复项一次。 For this purpose i use Distinct and Group By .为此,我使用DistinctGroup By

But Group By not working但是Group By不起作用

在此处输入图片说明

Wwhen you are grouping items property which you are using for group is going to 'Key' of the group.当您对用于组的项目属性进行分组时,该属性将成为组的“键”。 Replace Select(g => g.OrgnizationID) with Select(g => g.Key) it should help.Select(g => g.OrgnizationID)替换为Select(g => g.Key)应该会有所帮助。

To get distinct elements from OrganizationID array, you need not to perform GroupBy, you have to create new dynamic object using .Select() method.要从OrganizationID数组中获取不同的元素,您不需要执行 GroupBy,您必须使用.Select()方法创建新的动态对象。

Try below solution, this might help you尝试以下解决方案,这可能对您有所帮助

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)
                     .Select(g => g.OrgnizationID)  //Select only OrganizationID from complete object
                     .Distinct()   //Remove duplicates.
                     .ToArray()    //Convert IEnumerable<T> to Array
                 });


  return query.ToList();
}

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()}

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

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