My repository returns a list of Accounts.
我的存储库返回一个帐户列表。
Each account has a date and a MoneySpent decimal amount. So, I have my list of Accounts and in my controller I'm trying to process this list a little.
每个帐户都有一个日期和MoneySpent小数。所以,我有我的帐户列表,在我的控制器中,我正在尝试处理这个列表。
I want to have an object which contains the string name of all the months in my Account list and a Sum of all money spent for that month.
我想要一个对象,其中包含我的帐户列表中所有月份的字符串名称以及该月所花费的所有金额的总和。
Here is what I have tried:
这是我尝试过的:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Detail(int id)
{
var recentAccounts = accountRepository.GetAccountsSince(DateTime.Now.AddMonths(-6));
var monthlyTotals = from a in recentAccounts
group a by a.DateAssigned.Month.ToString("MMM") into m
select new
{
Month = m.Key,
MonthSum = m.Sum(a => a.MoneySpent)
};
return View();
}
Does this seem like the right way to calculate monthlyTotals?
这似乎是计算monthlyTotals的正确方法吗?
Also, I've been using strongly typed views with ViewModels for each view, so what type should I make monthlyTotals so I can add it as a field on my ViewModel and pass it to my View?
另外,我一直在为每个视图使用带有ViewModel的强类型视图,所以我应该将monthTotals设置为什么类型,以便我可以将它作为一个字段添加到我的ViewModel上并将其传递给我的View?
3 个解决方案
#1
Looks right to me.
看起来对我来说。
When I need to pass data like this to my view, I create a class for it. So your class would look like this:
当我需要将这样的数据传递给我的视图时,我为它创建了一个类。所以你的课程看起来像这样:
public class MonthlyTotal
{
public string Month { get; set; }
public decimal MonthSum { get; set; }
}
and your SELECT clause would look like this:
你的SELECT子句看起来像这样:
select new MonthlyTotal
{
Month = m.Key,
MonthSum= m.Sum(a => a.AmountAssigned)
}
#2
I would probably break out that logic into a service layer class holding business logic. Along with that, if the view is expecting a structure different than the model, you would transform your results in your service method returning a custom model type.
我可能会将该逻辑分解为包含业务逻辑的服务层类。除此之外,如果视图期望结构与模型不同,您将在服务方法中转换结果,返回自定义模型类型。
#3
Using an anonymous type won't work since the view code won't know what properties it has. I suggest creating a view-only model in the Models directory.
使用匿名类型将不起作用,因为视图代码将不知道它具有哪些属性。我建议在Models目录中创建一个仅查看模型。
public class MonthlySumModel
{
public string Month { get; set; }
public decimal Sum { get; set; }
}
Then create a new model value in the select statement:
然后在select语句中创建一个新的模型值:
select new MonthlySumModel
{
Month = m.Key,
Sum = m.Sum(a => a.MoneySpent)
};
You can then use this model as the type for the view.
然后,您可以将此模型用作视图的类型。
#1
Looks right to me.
看起来对我来说。
When I need to pass data like this to my view, I create a class for it. So your class would look like this:
当我需要将这样的数据传递给我的视图时,我为它创建了一个类。所以你的课程看起来像这样:
public class MonthlyTotal
{
public string Month { get; set; }
public decimal MonthSum { get; set; }
}
and your SELECT clause would look like this:
你的SELECT子句看起来像这样:
select new MonthlyTotal
{
Month = m.Key,
MonthSum= m.Sum(a => a.AmountAssigned)
}
#2
I would probably break out that logic into a service layer class holding business logic. Along with that, if the view is expecting a structure different than the model, you would transform your results in your service method returning a custom model type.
我可能会将该逻辑分解为包含业务逻辑的服务层类。除此之外,如果视图期望结构与模型不同,您将在服务方法中转换结果,返回自定义模型类型。
#3
Using an anonymous type won't work since the view code won't know what properties it has. I suggest creating a view-only model in the Models directory.
使用匿名类型将不起作用,因为视图代码将不知道它具有哪些属性。我建议在Models目录中创建一个仅查看模型。
public class MonthlySumModel
{
public string Month { get; set; }
public decimal Sum { get; set; }
}
Then create a new model value in the select statement:
然后在select语句中创建一个新的模型值:
select new MonthlySumModel
{
Month = m.Key,
Sum = m.Sum(a => a.MoneySpent)
};
You can then use this model as the type for the view.
然后,您可以将此模型用作视图的类型。