【问题标题】:UNIQUE columns with count using LINQ使用 LINQ 计数的唯一列
【发布时间】:2015-11-05 12:12:01
【问题描述】:

我有一个包含以下列的表格

Id
Address
City
Date
maxSeat
StateId [Foreign key with table State with columns Id,Name] 

我想编写一个 LINQ 查询来获取唯一 StateId 的列表及其计数

例如

State1 5 行

State2 3 行

State3 1 行

State4 5 行

List<int> uniqStates = dbContext.conflocations.Select(item => item.StateId)
                                              .Distinct().ToList();

这仅返回 stateId 的唯一列表。如何使用 LINQ 获得关联计数以及州名称?

【问题讨论】:

    标签: c# entity-framework linq linq-to-entities


    【解决方案1】:

    你需要GroupBy:-

    var uniqStates = dbContext.conflocations.GroupBy(item => item.StateId)
                              .Select(x => new 
                                      {
                                          StateId = x.Key,
                                          Count = x.Count()
                                      });
    

    【讨论】:

      【解决方案2】:

      您可以使用GroupBy 方法做到这一点:

      var uniqStates = dbContext.conflocations.GroupBy(item => item.StateId).Select(g=>new {StateId=g.Key,Count=g.Count()}).ToList();
      

      或者使用查询语法,你也可以这样做:

       var uniqStates= from conf in dbContext.conflocations
                       group conf by conf.StateId into g
                       select new {StateId=g.Key,
                                   Count=g.Count()
                                  }; 
      

      现在要获取州名,如果您的Conflocation 实体中有State 类型的导航属性,那么您可以执行以下操作:

      var uniqStates= from conf in dbContext.conflocations
                       group conf by conf.StateId into g
                       select new {StateId=g.Key,
                                   Name=g.FirstOrDefault().State.Name
                                   Count=g.Count()
                                  }; 
      

      更新

      如果您的StateWiseVenues 类具有与此查询投影结果的匿名类型相同的属性类型,那么您可以这样做:

      var uniqStates= from conf in dbContext.conflocations
                      group conf by conf.StateId into g
                      select new StateWiseVenues {StateId=g.Key,
                                                  Name=g.FirstOrDefault().State.Name
                                                  Count=g.Count()
                                                 }; 
       if(uniqStates !=null) 
       { 
         state_venues = uniqStates.ToList();
       } 
      

      【讨论】:

      • 在尝试使用代码 if(results ! =null) { state_venues = (List)results; }
      • 在这之后我得到这个异常方法'First'只能用作最终查询操作。请考虑在此实例中使用“FirstOrDefault”方法。
      • 嗨@JibinMathew,在使用FistOrDefault之后,你得到了你期望的结果吗?
      【解决方案3】:

      你需要对两个表进行jint:

      using( var dbContext=...)
      {
         var results = from c in dbContext.conflocations
                       from s in dbContext.States.Where(x=>x.Id = c.StateId).DefaultIfEmpty()
                       group new {c, s} by s.StateId into grp
                       select new { 
                                    StateId = grp.Key,
                                    StateName= grp.Max(x=>x.s.Name),
                                    Count = grp.Count()
                                  };
         ...
      }
      

      【讨论】:

      • 什么键??我得到构建错误密钥不存在
      • 这是组的密钥:s.StateId 在这种情况下
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多