group by using linq c# on entity framework

后端 未结 2 1646
名媛妹妹
名媛妹妹 2021-01-27 02:33

I need to group by Names and to sum all the instance of the name this is my code in the controller:

public class FansController : Controller
{
    private dbFan          


        
相关标签:
2条回答
  • 2021-01-27 03:01
    var result = from f in db.fans
                 group 1 by f.Name into g
                 select new { Name = g.Key, Amount = g.Count() };
    

    Now the @model should currently be:

    • IEnumerable<dynamic>. From: view with IEnumerable of object type in MVC: @model IEnumerable<dynamic>
    • or that you create a class containing the two properties.
    0 讨论(0)
  • 2021-01-27 03:06

    see this

    Selecting count in LINQ

    And try something like:

    var group = (from f in db.fans
                group f by a.Name into g
                select new { Name = g.Name, Count = g.Count() });
    
    0 讨论(0)
提交回复
热议问题