First, take a look at this thread
首先,看看这个帖子
LINQ to Dataset DBNULL problem / null reference exception
LINQ to Dataset DBNULL问题/ null引用异常
The link above provides you how to LEFT JOIN and then SELECT column that have NULL value in it without taking Exception error.
上面的链接提供了如何LEFT JOIN,然后是SELECT列,其中包含NULL值,而不会出现异常错误。
My problem is I want to COUNT the column is null in WHERE clause (after my LEFT JOIN, some NULL value pop up in the column), something like this:
我的问题是我想在WHERE子句中将COUNT列为null(在我的LEFT JOIN之后,列中弹出一些NULL值),如下所示:
int tmp1 =
(
from n in dt_query
join m in dt_query2
on n.Field<string>("VC_CLIENT_MAXID") equals m.Field<string>("VC_CHAT_MAXID") into nm
from m in nm.DefaultIfEmpty()
where
//string.IsNullorEmpty(m.Field<string>("VC_CHAT_STAFF"))
//DBNull.Value.Equals(m.Field<string>("VC_CHAT_STAFF"))
//m.IsNull("VC_CHAT_STAFF")
//object.Equals(m.Field<string>("VC_CHAT_STAFF"), xxx) <<< String xxx = null on above
select n.Field<string>("VC_CLIENT_MAXID")
).Count();
The commented lines are the solution I tried but it didn't work for me. Any help would be appreciated.
注释行是我尝试的解决方案,但它对我不起作用。任何帮助,将不胜感激。
2 个解决方案
#1
If what you're asking is how to count the number of records where the joined table is null, then give this a try.
如果您要问的是如何计算连接表为空的记录数,那么请尝试一下。
int tmp1 =
(
from n in dt_query
join m in dt_query2
on n.Field<string>("VC_CLIENT_MAXID")
equals m.Field<string>("VC_CHAT_MAXID") into nm
from LeftJoinM in nm.DefaultIfEmpty()
where
LeftJoinM == null
select n.Field<string>("VC_CLIENT_MAXID")
).Count();
If I misunderstood you, please let me know in a comment and I'll be happy to update.
如果我误解了你,请在评论中告诉我,我很乐意更新。
#2
I have not tested the following code..Hope this should work
我没有测试下面的代码..希望这应该工作
int tmp1 =
(
from n in dt_query
join m in dt_query2
on n.Field<string>("VC_CLIENT_MAXID")
equals m.Field<string>("VC_CHAT_MAXID") into nm
from temp in nm.where(t => t.Field<string>("VC_CLIENT_MAXID") ==null).DefaultIfEmpty()
select VC_CLIENT_MAXID= (temp== null) ? "" :n.Field<string>("VC_CLIENT_MAXID");
).Count();
#1
If what you're asking is how to count the number of records where the joined table is null, then give this a try.
如果您要问的是如何计算连接表为空的记录数,那么请尝试一下。
int tmp1 =
(
from n in dt_query
join m in dt_query2
on n.Field<string>("VC_CLIENT_MAXID")
equals m.Field<string>("VC_CHAT_MAXID") into nm
from LeftJoinM in nm.DefaultIfEmpty()
where
LeftJoinM == null
select n.Field<string>("VC_CLIENT_MAXID")
).Count();
If I misunderstood you, please let me know in a comment and I'll be happy to update.
如果我误解了你,请在评论中告诉我,我很乐意更新。
#2
I have not tested the following code..Hope this should work
我没有测试下面的代码..希望这应该工作
int tmp1 =
(
from n in dt_query
join m in dt_query2
on n.Field<string>("VC_CLIENT_MAXID")
equals m.Field<string>("VC_CHAT_MAXID") into nm
from temp in nm.where(t => t.Field<string>("VC_CLIENT_MAXID") ==null).DefaultIfEmpty()
select VC_CLIENT_MAXID= (temp== null) ? "" :n.Field<string>("VC_CLIENT_MAXID");
).Count();