NULL problem in string concatination in LINQ
-
Hi all, I have a table 'tbl_titles'. The content of table is like below.
id title subtitle cost
1 NULL NULL NULL
2 NULL 0
3 test subtest 100
4
5 test1 subtest1 500
6 NULL 0
7 test2 400
8 subtest2 300
9 test3 NULL 200
10 NULL subtest4 100i have to get output by concatinating title & subtitle. Like below
id name cost
3 test subtest 100
5 test1 subtest1 500
7 test2 400
8 subtest2 300
9 test3 200
10 subtest4 100for that i wrote query like below. var query = from t in dataContext.tbl_titles where t.title.Length >0 || t.subtitle.Length > 0 select new { name = t.title + t.subtitle }; but i am getting NULL values as name even though any one of the string is not null or empty. Please suggest me, how to write query inorder to get output like above. Thanks in advance.
-
Hi all, I have a table 'tbl_titles'. The content of table is like below.
id title subtitle cost
1 NULL NULL NULL
2 NULL 0
3 test subtest 100
4
5 test1 subtest1 500
6 NULL 0
7 test2 400
8 subtest2 300
9 test3 NULL 200
10 NULL subtest4 100i have to get output by concatinating title & subtitle. Like below
id name cost
3 test subtest 100
5 test1 subtest1 500
7 test2 400
8 subtest2 300
9 test3 200
10 subtest4 100for that i wrote query like below. var query = from t in dataContext.tbl_titles where t.title.Length >0 || t.subtitle.Length > 0 select new { name = t.title + t.subtitle }; but i am getting NULL values as name even though any one of the string is not null or empty. Please suggest me, how to write query inorder to get output like above. Thanks in advance.
Check title/subtitle is Null or not before calling its Length :cool:
Abhishek Sur My Latest Articles Working with Excel using MDAC
Basics on LINQ and Lambda Expressions
Create .NET Templates -
Hi all, I have a table 'tbl_titles'. The content of table is like below.
id title subtitle cost
1 NULL NULL NULL
2 NULL 0
3 test subtest 100
4
5 test1 subtest1 500
6 NULL 0
7 test2 400
8 subtest2 300
9 test3 NULL 200
10 NULL subtest4 100i have to get output by concatinating title & subtitle. Like below
id name cost
3 test subtest 100
5 test1 subtest1 500
7 test2 400
8 subtest2 300
9 test3 200
10 subtest4 100for that i wrote query like below. var query = from t in dataContext.tbl_titles where t.title.Length >0 || t.subtitle.Length > 0 select new { name = t.title + t.subtitle }; but i am getting NULL values as name even though any one of the string is not null or empty. Please suggest me, how to write query inorder to get output like above. Thanks in advance.
use the following code snippets:
var query = from t in dataContext.tbl_titles
where
(
(!string.isNullOrEmpty(t.title) && t.title.Length > 0)
||
(!string.isNullOrEmpty(t.subtitle) && t.subtitle.Length > 0)
)
select new { name = t.title + t.subtitle };Regards, - Kunal Chowdhury (My Blog)
-
use the following code snippets:
var query = from t in dataContext.tbl_titles
where
(
(!string.isNullOrEmpty(t.title) && t.title.Length > 0)
||
(!string.isNullOrEmpty(t.subtitle) && t.subtitle.Length > 0)
)
select new { name = t.title + t.subtitle };Regards, - Kunal Chowdhury (My Blog)
Thanks for your reply.
-
Check title/subtitle is Null or not before calling its Length :cool:
Abhishek Sur My Latest Articles Working with Excel using MDAC
Basics on LINQ and Lambda Expressions
Create .NET TemplatesThanks for your reply. I will go through it.
-
Thanks for your reply.
here is sample
var query = from t in dataContext.Users
let fname = t.fname != null ? t.fname : " "
let lname = t.lname != null ? t.lname : " "
select new {name = fname.Tostring()+ " "+lname.ToString()};