csv 为什么这个Linq查询绑定到Blazor参数,不返回任何东西?

hlswsv35  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(89)

我有一个Linq查询,试图根据位于CSV文件中的选定膳食类型过滤数据。CSV文件正在由CsvHelper处理,并由相关模型文件表示。
如下图所示,当我选择早餐(或午餐、晚餐或小吃)时,它不返回任何数据。
Linq查询:

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膳食选择剃须刀内容:

<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;


EternityDataView组件运行的图片,早餐选择为SelectedMeal:

EternityDataViewAllMeals组件运行的图片,其中所有膳食都被选为SelectedMeal:



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,


我有点困惑。正如你在图片中看到的,你可以看到value属性显示在下拉列表的下面,我用它作为一个调试工具。在Linq查询中,我设置了断点,发现SelectedMeal为null。所以我不太确定那里发生了什么。值得注意的是,对于所有的餐,EternityDataViewAllMeals中的Linq查询只是选择所有内容,而不关心日期。我不知道为什么SelectedMeal为null。
也许我需要分组?

fd3cxomn

fd3cxomn1#

我设法解决了这个问题,改变了我的做事方式,并管理所选膳食类型和所选时间段的变化都在NutritionDataView.razor.cs上,而不是每个所选时间段都有单独的组件。

相关问题