How to concatenate two columns in linq to sql query's select projection
c#, linq, linq-to-sql
Solution
You can use the `??` operator as follows:
from s in test
select new
{
Phone = (s.PhoneCountryCode ?? "") + (s.PhoneNumber ?? "") + (s.PhoneExtension ?? "")
}
Problem
1- I use linq to sql to query a database table. 2- In my actual table, I store Phone Country Code, Phone Number and Phone Extension in different columns. 3- When I get the data I need Phone to be equal to concatenation of Phone Country Code, Phone Number and Phone Extension. 4- For some records, any of these 3 columns might have null values. 5- If one column is null, then the whole concatenation yields null. ``` from s in test select new{ Phone = s.PhoneCountryCode + s.PhoneNumber + s.PhoneExtension } ``` 6- I tried the following, but didn't work. Still yields null. ``` from s in test select new{ Phone = s.PhoneCountryCode == null ? "" : s.PhoneCountryCode + s.PhoneNumber == null ? "" : s.PhoneNumber + s.PhoneExtension == null ? "" : s.PhoneExtension } ```