1

我有以下 linq 表达式,可让我加入两个表,按 a 对它们进行DSCID分组,然后获取分组值的计数:

var qryGeoApppendCount =
              from a in append
              join g in geo
              on a.Field<string>("RNO")
              equals g.Field<string>("RNO")
              group g by g.Field<int>("DSCID") into appendGeo
              select new
              {
                DscId = appendGeo.Key,
                DscIdCount = appendGeo.Count()
              };

我需要通过仅选择大于 1 的计数来更进一步。我尝试了这样的事情:

select new
{
    DscId = appendGeo.Key,
    DscIdCount = appendGeo.Count(n => n.Count > 1)
};

但这没有用。我需要能够在qryGeoAppendQuery返回计数 > 1 的记录时抛出错误,因此理想情况下,查询将包含在 if 语句中。

4

4 回答 4

3
var qryGeoApppendCount =
              (from a in append
              join g in geo
              on a.Field<string>("RNO")
              equals g.Field<string>("RNO")
              group g by g.Field<int>("DSCID") into appendGeo
              select new
              {
                DscId = appendGeo.Key,
                DscIdCount = appendGeo.Count()
              })
              .Where(a => a.DscIdCount > 1);
于 2010-10-25T18:32:26.533 回答
1

你就不能...

select new
{
  DscId = appendGeo.Key,
  DscIdCount = appendGeo.Where(n => n.Count > 1).Count()
};

或者如果你只是想知道是否存在...

select new
{
  DscId = appendGeo.Key,
  ThrowException = appendGeo.Any(n => n.Count > 1)
};
于 2010-10-25T18:32:58.643 回答
0
var qryGeoApppendCount =
          from a in append
          join g in geo
          on a.Field<string>("RNO")
          equals g.Field<string>("RNO")
          group g by g.Field<int>("DSCID") into appendGeo
          where appendGeo.Count() > 1
          select new
          {
            DscId = appendGeo.Key,
            DscIdCount = appendGeo.Count()
          };

你不能在选择之前添加一个where子句吗?它在我的示例中有效,但我不确定没有看到数据。

于 2010-10-25T18:39:22.430 回答
0
"SELECT  [SubsNumber], sum([Usage_MB])/1024 as GB FROM [VASCDR].[dbo].[nrmSubsDailyMBUsage] where SubsNumber ='" + textBox1.Text.Trim() + "'" group by [SubsNumber] ;

是如何使用 c#

于 2013-03-12T07:08:03.040 回答