Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code .It gives error on using firstOrdefault () in linq query
C#
var query = (from r in context.JobSeekerRegistration
        select new
        {
            ID = r.ID,
            RegistrationNumber = r.RegistrationNumber,
            NCOCode = r.NCOCategory.Name != null ? r.NCOCategory.Name:
                    r.JobSeekerWorkDetails.OrderByDescending(a=>a.JobSeekerID).
                    FirstOrDefault().NCOCategory.Name,
            Status = r.ApplicationStatus.Status,
            Name = r.Name,
            CreatedOn = r.CreatedOn,
            StatusID = r.StatusID,
            LocationID = r.LocationID
         });
Posted
Updated 29-Oct-14 0:15am
v2
Comments
George Jonsson 29-Oct-14 6:17am    
Do a check if the result of FirstOrDefault() is null.
You can also change to use First() to see if you get a new error.
Maciej Los 29-Oct-14 6:18am    
Please, be more specific and provide more details, for example complete error message.
Nathan Minier 29-Oct-14 7:44am    
Fluent embedded in select style makes my head hurt a little.

Try:
NCOCode = r.NCOCategory.Name != null ? r.NCOCategory.Name:
r.JobSeekerWorkDetails
.Select(x => x.NCOCategory.Name)
.OrderByDescending(a=>a.JobSeekerID)
.FirstOrDefault(),

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900