[英]How can i apply LINQ query on nested collections by checking each collection is null or not using LINQ with asp.net C#?
如何通過將null與asp.net c#一起使用LINQ來檢查是否為null來訪問嵌套集合? 因為我沒有空餐計划時會得到空異常?
因為我沒有空餐計划時會得到空異常?
var hotelRoomBooking = (from n in roomsPart.DefaultIfEmpty()
//where n.roomRates != null
from rates in n.roomRates.DefaultIfEmpty()
//where rates.roomRate != null
from roomR in rates.roomRate.DefaultIfEmpty()
//where roomR.mealPlans != null
from mealP in roomR.mealPlans.mealPlan
//where mealP.value != null
from link in roomR.links.link
select new
{
n.id,
n.rooms.checkInDate,
n.rooms.checkOutDate,`enter code here`
n.rooms.totalCostOfRooms.totalCostInclusive.currency,
roomR.averageNightlyRate.value,
MealPlan = mealP.value != null ? mealP.value : string.Empty,
n.stayDetails.noOfRooms,
n.location.city,
n.location.code
}).ToList();
是的,您將獲得NullReferenceException。 如果要排除具有空值的行,請嘗試使用join
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.