zoukankan      html  css  js  c++  java
  • linq to sql 左联接出错,未将对象引用设置到实例

                var result = from a in model
                             join b in orderDetailModel on a.FoodMenuID equals b.FoodMenuID into g
                             from b in g.DefaultIfEmpty()
                             select new Model.WebApiModel.WMFoodMenu
                             {
                                 BarginPrice = a.BarginPrice,
                                 Credits = a.Credits,
                                 FoodMenuCategoryID = a.FoodMenuCategoryID,
                                 FoodMenuID = a.FoodMenuID,
                                 RestaurantID = a.RestaurantID,
                                 FoodMenuName = a.FoodMenuName,
                                 SaleCount = b != null ? b.FoodMenuSaleCount : 0,
                                 ImgUrl100 = a.ImgUrl100,
                                 ImgUrl150 = a.ImgUrl150,
                                 IsRecommend = a.IsRecommend,
                                 Price = a.Price
                             };
    

     这里,必须要注意一点是:SaleCount = b != null ? b.FoodMenuSaleCount : 0,这里如果SaleCount字段不允许为空,必须先判定。

    如果写成 SaleCount = b.FoodMenuSaleCount,当右边数据为空时,将出现未将对象引用设置对实例的错误

  • 相关阅读:
    Linux_day01_primaryCommand
    Variational auto-encoder VS auto-encoder
    python yield generator 详解
    Paper Writing
    DTU_AI lecture 09
    DTU_AI lecture 08
    Attention mechanism
    Energy Journals
    TF + pytorch学习
    expRNN
  • 原文地址:https://www.cnblogs.com/jys509/p/3576406.html
Copyright © 2011-2022 走看看