group by using linq c# on entity framework

后端 未结 2 1647
名媛妹妹
名媛妹妹 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. From: view with IEnumerable of object type in MVC: @model IEnumerable
    • or that you create a class containing the two properties.

提交回复
热议问题