开发者

LINQ to Dataset Left Join

Sorry if this question has already been raised, but after hours of searching, i cant seem to find the way on how to fix this. Im trying to create a left join on 1 table but when i try to return a column on the other table, a null reference error was raised. tried some workarounds but still doesn't work.

here is my code.

from t1 in ds.TABLE1
join t2 in ds.TABLE1
on t1.COL2 equals t2.COL1 into j1
from t3 in j1.DefaultIfEmpt开发者_运维技巧y()
select new { 
     t1.COL5,
     t3.COL6
};

If i try to display all columns from t1 all works great, but once i display cols from t3 then error appears. It seems that null rows from t3 is causing the error. How can i determine or rather prevent null rows from t3 to be displayed? Tried using null and dbnull but still no success.

I appreciate any help. thanks


You need to handle cases where t3 is null.

For example:

from t1 in ds.TABLE1
join t2 in ds.TABLE1
on t1.COL2 equals t2.COL1 into j1
from t3 in j1.DefaultIfEmpty()
select new { 
     t1.COL5,
     Col6: t3 == null ? null : t3.COL6
};


Add a where clause

from t1 in ds.TABLE1
join t2 in ds.TABLE1
on t1.COL2 equals t2.COL1 into j1
from t3 in j1.DefaultIfEmpty()
where t3 != null
select new { 
     t1.COL5,
     t3.COL6
};


Very sorry, but forgot to mention that one of the columns raising the error is a calculated column (datacolumn with expressions). I changed the column's NULLVALUE property from THROW EXCEPTION to (Null) and used the inline if in my select clause just as slacks mentioned and it solved my problem.

Just wondering, why does linq doesn't have any ways to trap DBNULL values? It can trap null but not dbnull? any difference?

Anyways, my problem was solved.

Thanks for you help guys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜