使用选择和分组依据的实体框架核心

时间:2020-02-01 12:56:59

标签: c# entity-framework asp.net-core entity-framework-core

我想在查询中使用val listview1=findViewById<ListView>(R.id.kontaktliste) select

groupby

但是我在_context.UserChart_Tbl .Where(uc => uc.UserID == "6") .GroupBy(g => g.OrgnizationID) .Select(g => g.OrgnizationID) .ToArray(); 中遇到了错误

错误CS1061'IGrouping'不包含'OrgnizationID'的定义,并且找不到可以接受的扩展方法'OrgnizationID'接受类型为'IGrouping'的第一个参数(您是否缺少using指令或程序集引用?)

我该如何解决这个问题?

更新

此代码解决的错误

select

完整代码

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

还有

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

public class DoctorDropDoenViewModel { public string DoctorID { get; set; } public string DoctorFullName { get; set; } public int[] OrgID { get; set; } } 可以有多个DoctorID,也可以重复。我只想显示重复项一次。为此,我使用OrgIDDistinct

但是Group By无效

enter image description here

3 个答案:

答案 0 :(得分:0)

当您将用于分组的项目属性分组时,将转到该组的“键”。将=SUM(N(FREQUENCY(IF(($B$1:$B$20=G6)*($C$1:$C$20="V1")*($D$1:$D$20="f"),$E$1:$E$20),$E$1:$E$20)>0)) 替换为Select(g => g.OrgnizationID)会有所帮助。

答案 1 :(得分:0)

要从OrganizationID数组中获取不同的元素,无需执行GroupBy,必须使用.Select()方法创建新的动态对象。

尝试以下解决方案,这可能会对您有所帮助

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

答案 2 :(得分:0)

您可以使用以下代码

_context.UserChart_Tbl.Where(uc => uc.UserID == u.Id)
.Where(x=>x.DoctorID==1).Select(g => g.OrgnizationID).Distinct().ToArray()}