mare
mare

Reputation: 13083

Concatenate NULL and string in Linq to entities query

This query actually works but returns new objects with ClientName set to null where FirstName or Lastname is null (any one of the two). How can I get around that? I would like to have an empty string instead of null in those rows.

var clients =
                    from client in _repository.GetAll()
                    where (client.Firstname.StartsWith(query) || client.Lastname.StartsWith(query))
                    select new
                            {
                                ClientName = (client.Firstname + " " + client.Lastname).Trim(),
                                client.Firstname,
                                client.Lastname,
                                client.Address1,
                                client.Address2,
                                client.client_id,
                                client.PrettyId,
                                client.PostCode.postalcode,
                                client.PostCode.postname
                            };

Upvotes: 2

Views: 3894

Answers (1)

n8wrl
n8wrl

Reputation: 19765

((client.Firstname ?? "") + " " + (client.Lastname ?? "")).Trim();

Upvotes: 11

Related Questions