Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得第二个默认值?_C#_Linq_Linq To Entities - Fatal编程技术网

C# 如何获得第二个默认值?

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,

我有一个简单的linq lambda语句

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";