2011-02-15 23 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