Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Linq选择/加入自定义返回类型_C#_Linq_Join_Lambda - Fatal编程技术网

C# C Linq选择/加入自定义返回类型

C# C Linq选择/加入自定义返回类型,c#,linq,join,lambda,C#,Linq,Join,Lambda,我有以下linq样本: List<FoodOrderItem> foodOrderItems = foodItemsWithPricesDbResult.ResultValue; List<FoodOrderItem> orderItemsWithDiscounts= (from orderItem in order.Items join foodOrderItem in foodOrderItems on orderItem.FoodId equals foodO

我有以下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之类的函数来更新和返回结果,如何以内联样式编写select MergeOrderAndFoodFormation FoodOrderItem、orderItem部分?我的意思是,如果我想在不使用外部函数的情况下编写代码,如何更改selectmergeorderandfoodformationfoodorderItem、orderItem段

2-如何使用Lambda表示法编写此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 new FoodOrderItem
                                               {
                                                   DiscountValue = orderItem.DiscountValue,
                                                   DiscountKind = orderItem.DiscountKind,
                                                   CurrentOrderedCount = orderItem.Count
                                               }).ToList();

您可以在那里自己创建新对象。

您正在修改源集合中的对象,这在使用Linq时通常不是最佳做法。它要求您使用lambda语法内联函数,如下所示:

order.Items.Join(foodOrderItems, oi=>oi.FoodId, foi=>foi.Id, (oi, foi) => new {oi, foi})
           .Select(j => {
                          j.foi.DiscountValue = j.oi.DiscountValue;
                          j.foi.DiscountKind = j.oi.DiscountKind;
                          j.foi.CurrentOrderedCount = j.oi.Count;   
                          return j.foi;                         
                        })
            .ToList()

请注意,在lambda语法中,Join比IMHO更难看,内联函数并不能为您带来很多好处。

这是在创建新的FoodOrderItem对象,而不是更新原始对象的属性。我怀疑原始的FoodOtherItem有其他属性,使用此方法可能会丢失。正如@DStanley注意到的,还有其他一些字段会以这种方式丢失,我不想以这种方式编写它,因为将来通过将每个新字段添加到我的对象中,我需要逐个更新传递字段的所有代码。@VSB抱歉,我想这是不可能的,因为LINQ只提供新的列表进行迭代。它无法对现有函数执行任何操作。为什么要内联函数?您是否希望它能消除查询无法转换为SQL的错误?@DStanley我想学习语法,我知道上面的语法更具可读性。您希望它能够消除查询无法转换为SQL的错误,这是什么意思??你能解释一下吗?如果你没有遇到类似的错误,那就不用担心了。这些问题通常是由于将linq查询转换为SQL时出错而产生的,内联通常无法修复。