2011-02-15 26 views
1

该查询实际工作但ClientName集返回null新的对象,其中名字或姓氏是null(两个中的任何一个)。我该如何解决这个问题?我想在这些行中有一个空字符串,而不是null串联NULL和Linq中的字符串实体查询

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

这就是为什么在设置字符串字段在数据库中为空的没有意义的原因。将FirstName和SecondName设置为NOT NULL,并忘记这些问题。 – LukLed 2011-02-16 04:24:55

回答

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

你忘了他``修剪` – Snowbear 2011-02-15 21:09:57