如何在linq表达式中处理null值 [英] How to handle null value in linq expression
本文介绍了如何在linq表达式中处理null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码
i have this code
var faSummaryForReviewReport = from p in dt.AsEnumerable()
where p.Field<string>("Section") == "Reports" && p.Field<string>("Key") == "FASummaryOnReviewReport"
select new
{
value = p.Field<string>("Value"),
description = p.Field<string>("Description"),
locked = p.Field<bool>("IsLocked")
};
foreach (var user in faSummaryForReviewReport)
{
cbFaSummary.SelectedItem.Value = user.value;
lblFASummary.Text = user.description;
chkFaSummary.Checked = user.locked;
}
i在locked = p.Field< bool>中获取空值(IsLocked)
如何处理空值plz帮助我
i am getting null value in " locked = p.Field<bool>("IsLocked")"
how to handle null value plz help me
推荐答案
如果IsLocked可以包含null值然后尝试使用可以为空的布尔值来转换字段的值:
If the IsLocked can contain a null value then try casting the value of the field using a nullable boolean:
...
locked = p.Field<bool?>("IsLocked")
...
你可以在Field方法中使用nullable boolean,但你需要布尔值来设置为Checked属性,这样你就可以检查null和set false(或根据您的要求为真)如下所示
You can use nullable boolean in Field method but you need boolean value to set as Checked property, so you can check for null and set false(or true according to your requirement) as below
locked = p.Field<bool?>("IsLocked")??false
如果哟你使用可以忽略的布尔值如下所示
if you use nullable boolean value as below
locked = p.Field<bool?>("IsLocked")
Set价值如下
Set value as below
chkFaSummary.Checked = user.locked.Value;
这篇关于如何在linq表达式中处理null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文