Rani Radcliff
Rani Radcliff

Reputation: 5084

Cannot convert method group 'FirstOrDefault' to non-delegate type

I am retrieving a List and need to get a specific record. I have tried doing it a couple of different ways, but each way produces an error. I receive the aforementioned error using the following code:

List<DashboardModel> dashboardData = CompanyClient.GetContractorRankingByMajorIDAndContractorID(majorId, contractorId);
DashboardModel SSQScore = new DashboardModel();
SSQScore = dashboardData.Where(x=>x.ModuleInstanceID == 1).FirstOrDefault;

The List is returned by the method (GetContractor...).

Any assistance is greatly appreciated!

Upvotes: 3

Views: 1938

Answers (2)

Abel
Abel

Reputation: 57159

SSQScore = dashboardData.Where(x => x.ModuleInstanceID == 1).FirstOrDefault;

You miss the parentheses, which says to the compiler "use this method", instead of calling the method. Change it to

SSQScore = dashboardData.Where(x => x.ModuleInstanceID == 1).FirstOrDefault();

Alternatively, you do not need to do Where(...).FirstOrDefault(), the latter can take a predicate expression. This is equivalent:

SSQScore = dashboardData.FirstOrDefault(x => x.ModuleInstanceID == 1);

Upvotes: 2

Christos
Christos

Reputation: 53958

This

SSQScore = dashboardData.Where(x=>x.ModuleInstanceID == 1).FirstOrDefault;

should had been written like this

SSQScore = dashboardData.FirstOrDefault(x=>x.ModuleInstanceID == 1);

or you could just correct you statement, by using (), after FirstOrDefault,

SSQScore = dashboardData.Where(x=>x.ModuleInstanceID == 1).FirstOrDefault();

Upvotes: 5

Related Questions