C# 分组依据、计数和 Lambda 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19285443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:37:34  来源:igfitidea点击:

Group by, Count and Lambda Expression

c#linqentity-frameworklambda

提问by N8K8

I am trying to translate the following query:

我正在尝试翻译以下查询:

SELECT STATE, COUNT(*)
FROM MYTABLE
GROUP BY STATE;

Into a lambda expression. I am using C# and EntityFramework, however it doesnt seem I can make it work. Here is what I have on my respository so far:

变成一个 lambda 表达式。我正在使用 C# 和 EntityFramework,但是我似乎无法让它工作。到目前为止,这是我的存储库中的内容:

public IEnumerable<object> PorcentajeState(Guid id)
{
    return _context.Sates.Where(a => a.Id == id)
                         .GroupBy(a => a.State)
                         .Select(n => new { n.StateId , n.Count() });
}

Of course it doesnt compile and I am lost after googling for 2 hours . Could you please help me?

当然它不会编译,我在谷歌搜索 2 小时后迷路了。请你帮助我好吗?

thanks in advance

提前致谢

采纳答案by p.s.w.g

There are two issues here:

这里有两个问题:

  1. The result of GroupBywill will be an enumerable of type IEnumerable<IGrouping<TKey, TSource>>. The IGroupinginterface only has one property you can access, Keywhich is the key you specified in the GroupByexpression, and implements IEnumerable<T>so you can do other Linq operations on the result.
  2. You need to specify a property name for the anonymous type if it cannot be inferred from a property or field expression. In this case, you're calling Counton the IGrouping, so you need to specify a name for that property.
  1. GroupBywill的结果将是一个可枚举类型IEnumerable<IGrouping<TKey, TSource>>。该IGrouping接口只有一个您可以访问的属性,Key即您在GroupBy表达式中指定的键,并实现了IEnumerable<T>您可以对结果执行其他 Linq 操作。
  2. 如果无法从属性或字段表达式推断出匿名类型,则需要为匿名类型指定属性名称。在这种情况下,您调用Count的是IGrouping,因此您需要为该属性指定一个名称。

Try this:

尝试这个:

public IEnumerable<object> PorcentajeState(Guid id)
{
    return _context.Sates.Where(a => a.Id == id)
                         .GroupBy(a => a.StateId)
                         .Select(g => new { g.Key, Count = g.Count() });
}

The equivalent in query syntax would be

查询语法中的等效项是

public IEnumerable<object> PorcentajeState(Guid id)
{
    return from a in _context.Sates
           where a.Id == id
           group a by a.StateId into g
           select new { a.Key, Count = g.Count() };
}

In either case, if you want the first property to be named StateIdinstead of Key, just change that to

在任何一种情况下,如果您希望第一个属性被命名StateId而不是Key,只需将其更改为

new { StateId = g.Key, Count = g.Count() }

回答by Ahsan Qureshi

This one is good

这个不错

public IEnumerable<object> PorcentajeState(Guid id)
    {
        return _context.Sates.Where(a => a.Id == id)
                             .GroupBy(a => a.StateId)
                             .Select(g => new { g.Key, Count = g.Count() });
    }

But try this.

但是试试这个。

public IEnumerable<object> PorcentajeState(Guid id)
        {
            return _context.Sates.Where(a => a.Id == id)
                                 .GroupBy(a => a.StateId)
                                 .Select(g => new { g.Key.StateId, Count = g.Count() });
        }