HOW TO GET SECONDORDEFAULT?

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
C# 
C#
 1806  View(s)
Ratings:
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'.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to get SecondOrDefault?

    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() + " ..."

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!