在Linq中将NULL和字符串连接到实体查询

时间:2011-02-15 21:07:45

标签: c# linq-to-entities

此查询实际上有效但返回ClientName设置为null的新对象,其中FirstName或Lastname为null(两者中的任何一个)。我怎么能绕过那个?我希望在这些行中有一个空字符串而不是null

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
                            };

1 个答案:

答案 0 :(得分:10)

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