关于Linq 中用select 处理Null或"" 的问题
有这样一条语句
cmbOutbMaterBNo.DisplayMember = "OutbMaterBNo";
cmbOutbMaterBNo.ValueMember = "OutbMaterBNo";
var cli = (from c in bContTab.GetAll_ContNoBySortCode("C")
where c.ContNo == cmbContNo.SelectedValue.ToString()
select c.SortCode ).Distinct().ToList();
cmbOutbMaterBNo.DataSource = cli;// 这是combox绑定
其中 bContTab.GetAll_ContNoBySortCode("C") 返回的事自定义类的list 如果select 中的c.sortcode 出现null 就会提示"数据为空! 不能对空值调用此方法或属性"
------解决方案--------------------
var cli = (from c in bContTab.GetAll_ContNoBySortCode("C")
where c.ContNo == cmbContNo.SelectedValue.ToString()
select c.SortCode==null?"":c.SortCode).Distinct().ToList();
------解决方案--------------------
var cli = (from c in bContTab.GetAll_ContNoBySortCode("C")
where c.ContNo == cmbContNo.SelectedValue.ToString()
select c.SortCode??"").Distinct().ToList();