C# 如何获得第二个默认值?
我有一个简单的linq lambda语句C# 如何获得第二个默认值?,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我有一个简单的linq lambda语句 Interactions = new BindableCollection<InteractionDTO>(ctx.Interactions.Where(x => x.ActivityDate > DateTime.Today) .Select(x => new InteractionDTO { Id = x.Id, ActivityDate = x.ActivityDate,
Interactions = new BindableCollection<InteractionDTO>(ctx.Interactions.Where(x => x.ActivityDate > DateTime.Today)
.Select(x => new InteractionDTO
{
Id = x.Id,
ActivityDate = x.ActivityDate,
subject = x.Subject,
ClientNames = x.Attendees.Count == 1 ? x.Attendees.FirstOrDefault().Person.CorrespondenceName :
x.Attendees.FirstOrDefault().Person.CorrespondenceName : "Multiple attendees"
}));
但我得到了这个错误:
方法“Skip”仅支持LINQ to实体中的排序输入。方法“OrderBy”必须在方法“Skip”之前调用
你可以试着先点餐,正如信息所暗示的那样 我不确定您的
与会者
课程是什么样子,但假设它有一个ID字段:
x.Attendees.OrderBy(a => a.ID)
.Skip(1)
.Select(a => a.Person.CorrespondenceName).FirstOrDefault() + " ..."
还有几点需要注意: 我交换了您的
Select
和FirstOrDefault
语句。按照您当前获得它的方式,如果FirstOrDefault()
返回null,则Person.RespondenceName
将抛出异常
但现在如果找不到任何记录,您将只得到“…
”。您可能需要调整第一个Where
子句以筛选出没有对应名称的记录,然后将FirstOrDefault()
更改为first()
编辑: 这将引导你朝着正确的方向前进(我希望如此)。这可能更符合您的要求,假设它实际上可以转换为有效的SQL语句:
ClientNames = x.Attendees.Any()
? x.Attendees.Count == 1
? x.Attendees.Select(a => a.Person.CorrespondenceName).FirstOrDefault()
: x.Attendees.Count == 2
? string.Join(", ", x.Attendees.OrderBy(a => a.ID).Take(2)
.Select(a => a.Person.CorrespondenceName).FirstOrDefault()
: string.Join(", ", x.Attendees.OrderBy(a => a.ID).Take(2)
.Select(a => a.Person.CorrespondenceName).FirstOrDefault() + " ..."
: "No client name available";
也许用Take(2)替换Skip(1)?^效果很好@GrantWinney谢谢你需要一个Skip(1)才能获得第二个或默认值。
ClientNames = x.Attendees.Any()
? x.Attendees.Count == 1
? x.Attendees.Select(a => a.Person.CorrespondenceName).FirstOrDefault()
: x.Attendees.Count == 2
? string.Join(", ", x.Attendees.OrderBy(a => a.ID).Take(2)
.Select(a => a.Person.CorrespondenceName).FirstOrDefault()
: string.Join(", ", x.Attendees.OrderBy(a => a.ID).Take(2)
.Select(a => a.Person.CorrespondenceName).FirstOrDefault() + " ..."
: "No client name available";