此查询实际上有效,但返回的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
};
最佳答案
((client.Firstname ?? "") + " " + (client.Lastname ?? "")).Trim();
关于c# - 将Linq中的NULL和字符串连接到实体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5009506/