【发布时间】:2020-06-25 15:14:38
【问题描述】:
我有以下 linq 示例:
List<FoodOrderItem> foodOrderItems = foodItemsWithPricesDbResult.ResultValue;
List<FoodOrderItem> orderItemsWithDiscounts= (from orderItem in order.Items
join foodOrderItem in foodOrderItems
on orderItem.FoodId equals foodOrderItem.Id
select mergeOrderAndFoodInformation(foodOrderItem,orderItem)).ToList();
.
.
.
private static FoodOrderItem mergeOrderAndFoodInformation(FoodOrderItem foodOrderItem, OrderItem orderItem)
{
foodOrderItem.DiscountValue = orderItem.DiscountValue;
foodOrderItem.DiscountKind = orderItem.DiscountKind;
foodOrderItem.CurrentOrderedCount = orderItem.Count;
return foodOrderItem;
}
此mergeOrderAndFoodInformation 函数更新输入foodOrderItem 对象的某些字段并返回更新后的foodOrderItem。
1- 如果我不想使用mergeOrderAndFoodInformation 之类的函数来更新和返回结果,如何以inline 样式编写select mergeOrderAndFoodInformation(foodOrderItem,orderItem) 部分?我的意思是,如果我想在不使用外部函数的情况下编写代码,如何更改 select mergeOrderAndFoodInformation(foodOrderItem,orderItem) 段?
2- 如何使用 Lambda 表示法编写此 linq 查询?
附:如果没有正确使用 inline 和 external 等关键字,请接受我的道歉。如有错误,敬请指正。
【问题讨论】:
-
为什么要内联函数?您是否希望它能够消除您的查询无法转换为 SQL 的错误?
-
@DStanley 我想学习语法,我知道上面的语法更具可读性。
Are you hoping that it will get rid of errors that your query can't be translated to SQL?是什么意思?能解释一下吗? -
如果您没有收到类似的错误,请不要担心。这些问题的出现通常是因为将 linq 查询转换为 SQL 时出错,而内联通常无法修复。