information.Add(string.Concat(Resources.Rooms.FeeIncludes, string.Join(", ", candidate.FeesAndDeposits.ResortFee.Inclusions.Select(x => viewData.ResortFeeInclusions.SingleOrDefault(y => y.Value == x.ToString()).Text).ToArray())));
这个如果viewData.ResortFeeInclusions.SingleOrDefault(y => y.Value == x.ToString()) 找不到值 就回返回 defalut 就是null 然后再取.Text就会报错 怎么写判断 非null时 才返回Text啊?LINQ 里嵌套 判断怎么写啊
我本来是想下面这样写 但是老报错啊
information.Add(string.Concat(Resources.Rooms.FeeIncludes, string.Join(", ", candidate.FeesAndDeposits.ResortFee.Inclusions.Select(
(x) =>{
List<string> lst=new List<string>();
var item=viewData.ResoartFeeInclusions.SingleOrDefault(y => y.Value == x.ToString());
if(item!=null)
{
lst.Add(item.Text);
}
return lst;
});
var item=viewData.ResoartFeeInclusions.where(y => string.IsNullOrEmpty(x)||y.Value == x.ToString()).FirstOrDefault()
or
var item=viewData.ResoartFeeInclusions.SingleOrDefault(y => string.IsNullOrEmpty(x) || y.Value == x.ToString());
三目运算。Select(x=>x.Text=="AAA"?"BBB":x.Text)
information.Add(string.Concat(Resources.Rooms.FeeIncludes, string.Join(", ", candidate.FeesAndDeposits.ResortFee.Inclusions.Select(x => viewData.ResortFeeInclusions.Where(y => y.Value == x.ToString()).Select(y => y.Text).SingleOrDefault().ToArray())));
谢谢 nice
其实我感觉可以后面跟where 一样一可以吧就是比较麻烦