Home > DeveloperSection > Forums > How to get SecondOrDefault?
Tanuj Kumar

Total Post:134

Points:940
Posted on    August-19-2014 3:16 AM

 C# C# 
Ratings:


 1 Reply(s)
 1290  View(s)
Rate this:

I have a simple linq lambda statement

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"

    }));

This will give me the first Client Name, I'm trying to have it appear First 2 attendees followed by dots. I tried this

ClientNames = x.Attendees.Count == 1 ?

             x.Attendees.FirstOrDefault().Person.CorrespondenceName :

             x.Attendees.FirstOrDefault().Person.CorrespondenceName +

             x.Attendees.Skip(1).FirstOrDefault().Person.CorrespondenceName + " ..."

But I get this error:

The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-19-2014 3:36 AM

Hi Tanuj, 

I'm not sure what your Attendees class looks like, but assuming it has an ID field:

x.Attendees.OrderBy(a => a.ID)

           .Skip(1)

           .Select(a => a.Person.CorrespondenceName).FirstOrDefault() + " ..."


Don't want to miss updates? Please click the below button!

Follow MindStick