【问题标题】:Group By query in mvc5mvc5中的分组查询
【发布时间】:2016-10-25 11:35:45
【问题描述】:

您好,我想在我的 MVC5 应用程序的 C# 中编写 sql Group by query。

在上面的图片中,我有我在 sql 中编写的查询分组。我想用 C# 前端编写。

我尝试在前端编写查询。但是我收到了图片中提到的错误。现在我想用 C# 编写 Group By 查询,并希望显示每个员工的计数(输出与第一张图片中提到的相同)。谁能帮我解决这个问题?

我的 ViewModel(仪表板视图模型)

public class DashboardViewmodel
{
    public List<CustomerTypeCountModel> CustomerTypesCountModels { get; set; }
    public List<View_VisitorsForm> Visits { get; set; }
    public CustomerTypeViewModel CustomerTypeViewModels { get; set; }
    public int sizingcount { get; set; }
    public int Processingcount { get; set; }

    //here i declared two properties
    public string EmployeeName { get; set; }
    public string EmployeeCount { get; set; }
}

我的控制器代码

[HttpGet]
public ActionResult SalesVisit()
{
    return View();
}

public ActionResult GetDatesFromSalesVisit(DashboardViewmodel dvm)
{
    var fromdate = Convert.ToDateTime(dvm.CustomerTypeViewModels.FromDate);
    var todate = Convert.ToDateTime(dvm.CustomerTypeViewModels.ToDate);

    List<View_VisitorsForm> empcount = new List<View_VisitorsForm>();

   if (DepartmentID == new Guid("47D2C992-1CB6-44AA-91CA-6AA3C338447E") &&
      (UserTypeID == new Guid("106D02CC-7DC2-42BF-AC6F-D683ADDC1824") ||
      (UserTypeID == new Guid("B3728982-0016-4562-BF73-E9B8B99BD501"))))

       {

    var empcountresult = db.View_VisitorsForm.GroupBy(G => G.Employee)
                          .Select(e => new
                          {
                              employee = e.Key,
                              count = e.Count()
                          }).ToList();

        empcount = empcountresult ;//this line i am getting error 
 }
   DashboardViewmodel obj = new DashboardViewmodel();

     return View("SalesVisit", obj);
}

【问题讨论】:

  • 还有..请提供sql和错误信息,不要冒充图片..
  • ASP.NET MVC 是一个 Web 框架。它与分组数据无关

标签: c# sql-server linq asp.net-mvc-5


【解决方案1】:

当您使用GroupBy 时,您会得到一个IEnumerable&lt;IGrouping&lt;Key,YourOriginalType&gt;&gt;,因此您没有.Employee.VisitingID 属性。

修改如下:

public class EmployeeCount
{
    public string Employee {get; set;}
    public int Count {get; set;}
}

List<EmployeeCount> result = db.View_VisitorsForm
               .Where(item => item.VisitingDate >= beginDate && item.VisitingDate < endDate)
               .GroupBy(G => G.Employee)
               .Select(e =>new EmployeeCount
               {
                   employee = e.Key,
                   count = e.Count()
               }).ToList();

//Now add the result to the object you are passing to the View

另外请记住,您不是在实例化 View_VisitorsForm 类型的对象,而是一个匿名对象,因此将结果分配给 empcount 但单独添加 FirstOrDefault 将无法编译 p>

要将此结构传递给视图并呈现它,请检查this question

【讨论】:

  • Gilad Green 我无法理解第一张图片,我需要向每个员工展示我的展示方式,你能详细解释一下
  • Gilafd Green 查看我的 sql 查询,因为我计算了员工人数和其他人数。就像我喜欢在前端显示员工计数一样
  • @Swetha - 这就是上面的查询。执行一下看看
  • Select count(VisitingID)As Count,Employee from View_VisitorForm where VisitingDate between '2016-10-01' and '2016-10-30' Group by Employee 这是我的sql查询
  • 好吧,我怎么称呼它,因为你给了我控制器代码我需要一个示例视图代码
【解决方案2】:

希望对你有帮助

 var query = db.View_VisitorsForm.Where(o => o.VisitingDate >= new DateTime(2016,10,01) && o.VisitingDate <= new DateTime(2016, 10, 30)).GroupBy(G => G.Employee)

foreach (var item in query)
                {
                    Console.WriteLine($"Employee Id {item.Key} : Count :{item.Count()}");
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-13
    • 2016-06-08
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多