首页 新闻 会员 周边

Linq的Select里怎么写判断条件啊?

-1
悬赏园豆:5 [已解决问题] 解决于 2014-12-25 09:30

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;

});

吴顶天的主页 吴顶天 | 初学一级 | 园豆:39
提问于:2014-09-02 10:44
< >
分享
最佳答案
0

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());

收获园豆:5
沦落的伤心人 | 菜鸟二级 |园豆:208 | 2014-09-02 11:28
其他回答(3)
0

三目运算。Select(x=>x.Text=="AAA"?"BBB":x.Text)

幻天芒 | 园豆:37175 (高人七级) | 2014-09-02 10:47
0
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())));
519740105 | 园豆:5810 (大侠五级) | 2014-09-02 11:03

谢谢 nice

支持(0) 反对(0) 吴顶天 | 园豆:39 (初学一级) | 2014-09-02 15:05
0

其实我感觉可以后面跟where 一样一可以吧就是比较麻烦

我是糖糖 | 园豆:446 (菜鸟二级) | 2014-10-19 19:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册