Note that there are some explanatory texts on larger screens.

plurals
  1. POlinq to entity framework custom collection class that inherits list(Of T)
    text
    copied!<p>OK, I'm not quite sure how to ask this, but basically I want to create a custom collection class that allows me to have properties on a generic list (Of T) such as:</p> <pre><code>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 &gt; 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 &gt; 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 </code></pre> <p>I want this class to be used when calling entity framework queries: </p> <pre><code> 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 </code></pre> <p>I'm assuming it would go something like this: </p> <pre><code> Partial Public Class FoodDiaryItemCollection Inherits List(Of FoodDiaryItem) Sub New() End Sub End Class </code></pre> <p>Error I'm getting:</p> <p><strong>Unable to cast object of type 'System.Collections.Generic.List`1[NutritionBridgeModel.FoodDiaryItem]' to type 'NutritionBridgeModel.FoodDiaryItemCollection'.</strong></p> <p>TIA</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload