我的项目是MVC5,使用EF 6.1,MS Sql。我使用以下方式生成列表:
var data = db.Nutritions
.Include(nutrition => nutrition.FoodGroup)
.Include(nutrition => nutrition.Meal)
.Include(nutrition => nutrition.Fruit)
.Include(nutrition => nutrition.Vegetable)
.Select(nutrition =>
new
{
MealID = nutrition.MealID,
MealName = nutrition.Meal.MealName,
FoodGroupID = nutrition.FoodGroupID,
FoodGroupName = nutrition.FoodGroup.FoodGroupName,
FoodTypeID = nutrition.FoodTypeID,
FoodName = nutrition.Fruit.Name,
//FoodName = nutrition.Vegetable.Name
}).ToList().AsEnumerable();
我的挑战是我想要获得FoodName,它可能来自两个表:水果和蔬菜。不同的是两个表中的FoodGroupID,Fruits FoodGroupID是1而Vegetable是6.有没有办法根据FoodGroupID更改FoodName,或者我应该使用Where FoodGroupID等于1或6生成两个列表,然后合并两个表?
答案 0 :(得分:0)
你试过吗
values = {
'a': 1,
'b': 2,
'c': 3,
}
test = 'cab'
test_sum = sum([values[c] for c in test]) # 6
FoodName = nutrition.FoodGroupID == 1 ? nutrition.Fruit.Name : FoodName = nutrition.Vegetable.Name
是1
属性的值,如果错误,请将其替换为正确的值。
答案 1 :(得分:0)
试试这个
FoodGroupID.Fruits
答案 2 :(得分:0)
我认为如果FoodGroupID
不是1也不是6,我也应该处理:
FoodName = nutrition.FoodGroupID == 1 ? nutrition.Fruit.Name : (nutrition.FoodGroupID == 6 ? nutrition.Vegetable.Name : null)