I have a Linq query that is attempting to filter data down based on a selected meal type which is located inside a CSV file. The CSV file is being processed by CsvHelper and is represented by a relevant model file.
As one can see below, when I select breakfast (or lunch, dinner or snacks), it returns no data.
Linq query:
public partial class EternityDataView : IGenerateNutritionalValues {
private NutritionDataView dataView = new();
public decimal? GetCalorieValues()
{
var calories = from c in FileUpload.records
where c.Meal == dataView.SelectedMeal
select c.Calories;
return calories.Sum();
}
NutritionDataView meal select razor content:
<div class="meal-select">
<label for="meal-select">Select meal type:</label>
<select name="meal-select" id="meal-select" @bind="SelectedMeal">
<option value="all-meals">All meals</option>
<option value="breakfast">Breakfast</option>
<option value="dinner">Dinner</option>
<option value="lunch">Lunch</option>
<option value="snacks">Snacks</option>
</select>
</div>
.......
// Switch for date range
case "eternity":
if (SelectedMeal == "all-meals")
{
<EternityDataViewAllMeals/>
}
else
{
<EternityDataView/>
}
break;
A picture of the EternityDataView component running with breakfast selected as the SelectedMeal:
A picture of the EternityDataViewAllMeals component running with all meals selected as the SelectedMeal:
Sample of the CSV:
Date,Meal,Calories,Fat (g),Saturated Fat,Polyunsaturated Fat,Monounsaturated Fat,Trans Fat,Cholesterol,Sodium (mg),Potassium,Carbohydrates (g),Fiber,Sugar,Protein (g),Vitamin A,Vitamin C,Calcium,Iron,Note
2023-03-02,Breakfast,105.0,0.4,0.1,0.1,0.0,0.0,0.0,1.2,422.4,27.0,3.1,14.4,1.3,5.0,17.1,0.6,1.7,
2023-03-02,Lunch,710.0,30.1,3.2,0.1,0.0,0.0,0.0,2.8,214.0,91.8,9.8,44.4,18.1,0.0,15.3,1.2,1.3,
2023-03-02,Dinner,699.8,29.4,7.8,2.4,6.2,0.2,19.8,268.3,180.4,87.2,3.4,28.6,22.6,12.7,84.1,15.8,2.0,
2023-03-02,Snacks,166.0,0.4,0.0,0.1,0.1,0.0,0.0,6.9,615.7,39.6,1.0,28.7,2.4,0.0,193.7,19.4,2.5,
I’m a bit confused. As you can see in the image, you can see that the value property is being shown below the drop down, which I used as a bit of a debugging tool. In the Linq query, I have set breakpoints to find that SelectedMeal is null. So I am not quite sure what is happening there. It’s worth noting that for all meals, the Linq query inside EternityDataViewAllMeals is just selecting everything and not wondering about the date. I haven’t got the slightest idea why SelectedMeal is null.
Maybe I need to groupby?
You seem to bind to a property
SelectedMeal
which is not the same asdataView.SelectedMeal
. Try binding todataView.SelectedMeal
or use theSelectedMeal
property.Your filters don’t match the data, you have
breakfast
as the value in your option but the data hasBreakfast
. You need to either match the case or do a case insensitive comparison.