Reputation: 89
I have table called Customers and this customer has Bank Details but Some customer they don't have,
When i Use Linq to sql it's Return Null value, like a below example Table
How to prevent this null into Default value 0 or String
Ex. Table
Customer ID Name bank name
----------- ------ --------------
J0002 John HSBC
K0001 Kethy SMC
L0003 Mike Null
S0004 Lilie Null
Thanks
Upvotes: 0
Views: 954
Reputation: 31458
Something like
var customers = (from c in Customers
select new Customer
(
c.CustomerId,
c.Name,
c.BankName ?? ""
)).ToList();
or
var result = Customer.Select(x => new
{
CustomerId = x.CustomerId,
Name = x.Name,
BankName = x.BankName.HasValue ? x.BankName : ""
}).OrderBy(p => p.Name).ToArray();
could do the trick. It would be easier if you showed your current query.
Upvotes: 1