EB.
EB.

Reputation: 2757

Convert to Decimal using Linq

I am trying to select the value of Sales from my Class QCA. I am using linq to query a collection of QCA's. I want the most recent one by Date When I try this code I get errors telling me It cannot convert IEnumerable to decimal. Where am I going wrong?

SalesTotal = (from q in QuickCreditAssessments orderby q.FinancialsDate ascending
                              select q.Sales).Skip(1).Take(1);

Upvotes: 0

Views: 392

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460018

If you want a single don't use Take but First/FirstOrDefault/Single/SingleOrDefault:

decimal salesTotal = (from q in QuickCreditAssessments 
                      orderby q.FinancialsDate ascending 
                      select q.Sales)
    .Skip(1)  // now you get the second to last by Date
    .FirstOrDefault();

As an aside, if you want the "most recent one by Date" you should use descending.

Upvotes: 6

Alex Voskresenskiy
Alex Voskresenskiy

Reputation: 2233

Take returns IEnumerable<T>, instead of Take in your case i'd use FirstOrDefault(), like this:

SalesTotal = (from q in QuickCreditAssessments orderby q.FinancialsDate ascending
                              select q.Sales).Skip(1).FirstOrDefault();

Upvotes: 2

Related Questions