MVC - 计入foreach循环,最佳实践

时间:2015-09-18 19:27:45

标签: c# asp.net-mvc foreach

我一直致力于在foreach循环中获取计数记录。我需要在一个页面上运行许多这些计数。我正在寻找最有效的方法来做到这一点。

我已经走到了这一步,但我不确定我是否走上了正确的道路。如果是,我如何将这些数据放入我的视图中。

视图模型

public class AgtLeadStatsListVM
{
    public string LoanAgent { get; set; }
    public DateTime LeadDate { get; set; }
    public int LeadDailyCt { get; set; }
    public int LeadWeeklyCt { get; set; }
    public int LeadMTDCt { get; set; }
    public int LeadYTDCt { get; set; }
    public IEnumerable<MWFUser> AgentList { get; set; }
    public virtual WebLead Lead { get; set; }
}

控制器

 var model = new AgtLeadStatsListVM();            
 {
 // Get Selected Agent's Information
 var AgentList = from l in db.MWFUsers
                 where (l.UserTitle == "Banker"
                 select l;

    foreach (var agent in AgentList)
    {

    // Daily Lead Count
    var LeadDailyCt = db.WebLeads.Count(x => (x.LoanAgent == agent.UserEmail)
    && (x.LeadDate >= todayDate && x.LeadDate <= todayEndDay));

    // Weekly Lead Count
    var LeadWeeklyCt = db.WebLeads.Count(x => (x.LoanAgent == agent.UserEmail)
                                         && x.LeadDate >= firstOfWeek
                                         && x.LeadDate <= todayEndDay);

    // Monthly Lead Count
    var LeadMTDCount = db.WebLeads.Count(x => (x.LoanAgent == agent.UserEmail)
                                         && x.LeadDate >= firstOfMonth
                                         && x.LeadDate <= todayEndDay);

   // YTD Lead Count
   var LeadYTDCount = db.WebLeads.Count(x => (x.LoanAgent == agent.UserEmail)
                                         && x.LeadDate >= firstOfMonth
                                         && x.LeadDate <= todayEndDay);
}
}

查看

@model LoanModule.ViewModels.AgtLeadStatsListVM

<div>
    @foreach (var item in Model.AgentList)
    {
    <p>@Model.LoanAgent</p>
    <p>@Model.LeadDailyCt</p>
    <p>@Model.LeadWeeklyCt</p>
    <p>@Model.LeadMTDCt</p>
    <p>@Model.LeadYTDCt</p>
    }

我在View:Object引用上收到此错误未设置为对象的实例。 (在线:@foreach(Model.AgentList中的var项目))

我错过了什么?

谢谢。

2 个答案:

答案 0 :(得分:3)

var model = new AgtLeadStatsListVM();末尾的分号表示您不再位于该行之后的对象初始值设定项中。您可能正在尝试的语法更多的是这些:

var agents = 
    from l in db.MWFUsers
    where l.UserTitle == "Banker"
    select l;

var model = new AgtLeadStatsListVM
{
    // Get Selected Agent's Information
    AgentList = agents.ToList(),
    // Daily Lead Count
    LeadDailyCt = agents.Sum(a => db.WebLeads.Count(
        x => (x.LoanAgent == a.UserEmail)
            && (x.LeadDate >= todayDate && x.LeadDate <= todayEndDay)))
    // ...
}

顺便说一下,如果你想在一次往返中获得所有这些信息,你可以使用这个基于group by的技巧。

var model = 
    (from agent in agents
    let webLeads = db.WebLeads.Where(x => x.LoanAgent == agent.UserEmail)
    group new{agent, webLeads} by 0 into g
    select new AgtLeadStatsListVM
    {
        // Get Selected Agent's Information
        AgentList = g.Select(e => e.agent).ToList(),
        // Daily Lead Count
        LeadDailyCt = g.Sum(e => e.webLeads.Count(x => x.LeadDate >= todayDate && x.LeadDate <= todayEndDay)),
        // ...
    }).FirstOrDefault();

更新

从您的评论中可以看出,这更像是您的目标:

var model = 
    (from agent in agents
    let webLeads = db.WebLeads.Where(x => x.LoanAgent == agent.UserEmail)
    select new AgtLeadStatsListVM
    {
        // Get Selected Agent's Information
        LoanAgent = agent.UserEmail,
        // Daily Lead Count
        LeadDailyCt = webLeads.Count(x => x.LeadDate >= todayDate && x.LeadDate <= todayEndDay),
        // ...
    }).ToList();

您的观看代码:

@model IEnumerable<LoanModule.ViewModels.AgtLeadStatsListVM>

<div>
    @foreach (var item in Model)
    {
      <p>@item.LoanAgent</p>
      <p>@item.LeadDailyCt</p>
      <p>@item.LeadWeeklyCt</p>
      <p>@item.LeadMTDCt</p>
      <p>@item.LeadYTDCt</p>
    }

AgentList属性应完全从模型中移除。

答案 1 :(得分:1)

  

我在View:Object引用中未收到此错误   对象的实例。 (在线:@foreach(var中的项目)   Model.AgentList))

AgentList为空。

此外,您还没有正确初始化您的模型。

具体来说,这行代码

var model = new AgtLeadStatsListVM();

创建一个AgtLeadStatsListVM类型的新对象,其中

  • LoanAgent为空
  • LeadDate 1/1/0001 12:00:00 AM
  • LeadDailyCt为0
  • LeadWeeklyCt为0
  • LeadMTDCt为0
  • LeadYTDCt为0
  • AgentList为空
  • WebLead是Lead

默认值,因为您未设置任何值。您可能希望使用对象初始值设定项,您不需要();。我们写这个:

var model = new AgtLeadStatsListVM
{
    LoadAgent = "Name of the LoadAgent",
    LeadDate = DateTime.Now.Utc,
    LeadDailyCt = agents.Sum(a => db.WebLeads.Count(
    x => (x.LoanAgent == a.UserEmail)
        && (x.LeadDate >= todayDate && x.LeadDate <= todayEndDay)))
    // Do the same for the rest of the corresponding properties.
}