C# 如何从列表中的列中选择可为空的值
如何从列表中的列中选择可为null的值 例如,假设我将一个数据集转换为如下所示的列表,而不是在客户端id(可空列)中传递一个值。我需要传递空值。我自己做了以下尝试:C# 如何从列表中的列中选择可为空的值,c#,linq,linq-to-dataset,C#,Linq,Linq To Dataset,如何从列表中的列中选择可为null的值 例如,假设我将一个数据集转换为如下所示的列表,而不是在客户端id(可空列)中传递一个值。我需要传递空值。我自己做了以下尝试: var reqlist = (from list in tableList.AsEnumerable() where (list.Field<int?>("ClientID") == clientID) && list.Fiel
var reqlist = (from list in tableList.AsEnumerable()
where (list.Field<int?>("ClientID") == clientID)
&& list.Field<bool>("VisibleToAdmin") == true
&& list.Field<bool>("Required") == true
select list.Field<string>("FieldName"));
var reqlist=(来自tableList.AsEnumerable()中的列表)
其中(list.Field(“ClientID”)==ClientID)
&&list.Field(“VisibleToAdmin”)==true
&&list.Field(“必需”)==true
选择list.Field(“FieldName”);
一,
var reqlist=(来自tableList.AsEnumerable()中的列表)
其中(list.Field(“ClientID”)==null)
&&list.Field(“VisibleToAdmin”)==true
&&list.Field(“必需”)==true
选择list.Field(“FieldName”);
二,
var reqlist=(来自tableList.AsEnumerable()中的列表)
其中(list.Field(“ClientID”)==(int?)(null))
&&list.Field(“VisibleToAdmin”)==true
&&list.Field(“必需”)==true
选择list.Field(“FieldName”);
三,
var reqlist=(来自tableList.AsEnumerable()中的列表)
其中(list.Field(“ClientID”)==(bool?)(null))
&&list.Field(“VisibleToAdmin”)==true
&&list.Field(“必需”)==true
选择list.Field(“FieldName”);
四,
var reqlist=(来自tableList.AsEnumerable()中的列表)
其中(list.IsNull(“ClientID”))
&&list.Field(“VisibleToAdmin”)==true
&&list.Field(“必需”)==true
选择list.Field(“FieldName”);
使用上述所有方法,将抛出一个
InvalidCastException
。将可空值与空值进行比较是完全合法的:
list.Field<int?>("ClientID") == null
// same as
!list.Field<int?>("ClientID").HasValue
第一步:发帖前检查问题的拼写。或在@DACrosby:+1之后进行编辑。在易读性方面有奇效。(@mohamed:hint,hint;)您确定您在
字段调用中使用的字段类型正确吗?我突然想到,出错也可能引发这种异常……谢谢拉兹别列佐夫斯基。它工作得很好。@mohamedKalithu,这很好。请将答案标记为已接受,以便其他人看到问题已解决
var reqlist = (from list in tableList.AsEnumerable()
where (list.Field<int?>("ClientID") == (int?)(null))
&& list.Field<bool>("VisibleToAdmin") == true
&& list.Field<bool>("Required") == true
select list.Field<string>("FieldName"));
var reqlist = (from list in tableList.AsEnumerable()
where (list.Field<int?>("ClientID") == (bool?)(null))
&& list.Field<bool>("VisibleToAdmin") == true
&& list.Field<bool>("Required") == true
select list.Field<string>("FieldName"));
var reqlist = (from list in tableList.AsEnumerable()
where (list.IsNull("ClientID"))
&& list.Field<bool>("VisibleToAdmin") == true
&& list.Field<bool>("Required") == true
select list.Field<string>("FieldName"));
list.Field<int?>("ClientID") == null
// same as
!list.Field<int?>("ClientID").HasValue
int? clientID = null;
var reqlist = from r in tableList.AsEnumerable()
where r.Field<int?>("ClientID") == clientID &&
r.Field<bool?>("VisibleToAdmin") == true &&
r.Field<bool?>("Required") == true
select r.Field<string>("FieldName");