好的,我不太确定如何问这个问题,但基本上我想创建一个自定义集合类,它允许我在通用列表(Of T)上拥有属性,例如:
Public ReadOnly Property TotalCalories() As Decimal
Get
Dim decTotal As Decimal = 0D
For Each thisFoodDiaryItem As FoodDiaryItem In Me
Dim thisNutValue As NutritionalData = (From nd In thisFoodDiaryItem.Food.NutritionalDatas Where nd.NutritionalDefinitionID = 208).FirstOrDefault
If thisFoodDiaryItem.Food.FoodWeights.Count > 0 Then
Dim decWeight As Decimal = thisFoodDiaryItem.Food.FoodWeights(0).WeightInGrams
If Not thisNutValue Is Nothing Then
Dim decCalories As Decimal = Math.Round((thisNutValue.Value * (decWeight / 100)) * thisFoodDiaryItem.Amount)
decTotal += decCalories
End If
End If
Next
Return decTotal
End Get
End Property
Public ReadOnly Property TotalSugars() As Decimal
Get
Dim decTotal As Decimal = 0D
For Each thisFoodDiaryItem As FoodDiaryItem In Me
Dim thisNutValue As NutritionalData = (From nd In thisFoodDiaryItem.Food.NutritionalDatas Where nd.NutritionalDefinitionID = 269).FirstOrDefault
If thisFoodDiaryItem.Food.FoodWeights.Count > 0 Then
Dim decWeight As Decimal = thisFoodDiaryItem.Food.FoodWeights(0).WeightInGrams
If Not thisNutValue Is Nothing Then
Dim decCalories As Decimal = Math.Round((thisNutValue.Value * (decWeight / 100)) * thisFoodDiaryItem.Amount)
decTotal += decCalories
End If
End If
Next
Return decTotal
End Get
End Property
我希望在调用实体框架查询时使用此类:
Protected Sub BindFoodDiaryItems()
CurrentFoodDiaryItems = New FoodDiaryItemCollection
CurrentFoodDiaryItems = (From fdi In db.FoodDiaryItems Where fdi.PersonID = CurrentSiteUser.PersonID And fdi.EntryDate = CurrentDiaryDate).ToList
If AllMeals Is Nothing Then
AllMeals = (From m In db.Meals).ToList
End If
lvMealsA.DataSource = AllMeals
lvMealsA.DataBind()
lblTotalCalories.Text = CurrentFoodDiaryItems.TotalCalories.ToString
lblTotalSugars.Text = CurrentFoodDiaryItems.TotalSugars.ToString
lblTotalFats.Text = CurrentFoodDiaryItems.TotalFat.ToString
lblTotalProtein.Text = CurrentFoodDiaryItems.TotalProtein.ToString
lblTotalCarbs.Text = CurrentFoodDiaryItems.TotalCarbs.ToString
End Sub
我假设它会像这样:
Partial Public Class FoodDiaryItemCollection
Inherits List(Of FoodDiaryItem)
Sub New()
End Sub
End Class
我得到的错误:
无法将类型为'System.Collections.Generic.List`1 [NutritionBridgeModel.FoodDiaryItem]'的对象转换为'NutritionBridgeModel.FoodDiaryItemCollection'。
TIA
答案 0 :(得分:1)
.Net Framework暴露了许多令人敬畏的扩展方法,如Sum,Average等......
http://msdn.microsoft.com/en-us/library/bb341635
示例:
class Program
{
static void Main(string[] args)
{
var sweetList = new List<int> { 4, 123, 5, 23, 42 };
Console.WriteLine("Average: {0}", sweetList.Average());
Console.WriteLine("Sum: {0}", sweetList.Sum());
var listOfCoolObjects = new List<CoolObject>();
listOfCoolObjects.Add(new CoolObject { Name = "1434", NeatValue = 42 });
listOfCoolObjects.Add(new CoolObject { Name = "asdf", NeatValue = 5 });
listOfCoolObjects.Add(new CoolObject { Name = "fgsdfg", NeatValue = 99 });
listOfCoolObjects.Add(new CoolObject { Name = "qwerty", NeatValue = 1345 });
listOfCoolObjects.Add(new CoolObject { Name = "casc", NeatValue = 111 });
Console.WriteLine("Average: {0}", listOfCoolObjects.Average(x => x.NeatValue));
Console.WriteLine("Sum: {0}", listOfCoolObjects.Sum(x => x.NeatValue));
}
}
class CoolObject
{
public string Name { get; set; }
public int NeatValue { get; set; }
}
在您的代码中,您可以执行以下操作:
var sum = (From fdi In db.FoodDiaryItems Where fdi.PersonID = CurrentSiteUser.PersonID And fdi.EntryDate = CurrentDiaryDate).Sum(fdi => fdi.SomeValueYouWantToSum)
我知道你是用VB编写的,但我根本不懂VB,所以请原谅最后一个代码块中的语法错误。