我正在努力做到这一点:
var customerSearchResult = customers.GroupBy(
x => new {
x.CustomerID,
x.email,
x.CreatedOn,
x.FirstName,
x.LastName,
x.Profile == null ? -1 : x.Profile.Value
})
.Select(csr => new CustomerSearchResult
{
CustomerID = csr.Key.CustomerID,
Email = csr.Key.email,
CreatedOn = csr.Key.CreatedOn
});
我正在
错误CS0746无效的匿名类型成员声明符。匿名类型 必须使用成员作业,简单名称或成员声明成员 会员访问。
由于此行x.Profile == null ? -1 : x.Profile.Value
个人资料可以为空。
知道怎么做吗?
答案 0 :(得分:6)
在Anonymous
class:
var customerSearchResult = customers.GroupBy(
x => new {
x.CustomerID,
x.email,
x.CreatedOn,
x.FirstName,
x.LastName,
Profile = x.Profile == null ? -1 : x.Profile.Value
})
.Select(csr => new CustomerSearchResult
{
CustomerID = csr.Key.CustomerID,
Email = csr.Key.email,
CreatedOn = csr.Key.CreatedOn
});
正如@ Abion47在评论中提到的那样,你可以简化你的任务:
Profile = x.Profile?.Value ?? -1