linq to sql 左联接出错,未将对象引用设置到实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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,当右边数据为空时,将出现未将对象引用设置对实例的错误

linq to sql 左联接出错,未将对象引用设置到实例,布布扣,bubuko.com

linq to sql 左联接出错,未将对象引用设置到实例

上一篇:关于mysql中无法显示中文的完美解决方案


下一篇:[转载]Unity3D 游戏引擎之使用C#语言建立本地数据库(SQLITE)