Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable return First

Tags:

c#

linq

I have the following code:

    public IEnumerable<Report> GetReport(int reportId)
    {
        var dbReport = dbContext.ReportsTbl.Where(w =>w.ID == reportId);
        return dbReport;

   }

What I like to do though us to get First

If I do:

    public IEnumerable<Report> GetReport(int reportId)
    {
        var dbReport = dbContext.ReportsTbl.First(w =>w.ID == reportId);
        return dbReport;
   }

How do I get it to do First(). It is complaining about it being IEnumerable.

like image 848
Nate Pet Avatar asked Jul 29 '26 04:07

Nate Pet


1 Answers

You need to change the method signature to return only a single object instead of a collection:

public Report GetReport(int reportId)
{
    var dbReport = dbContext.ReportsTbl.First(w =>w.ID == reportId);
    return dbReport;
}

If for some reason you do actually want a collection containing only the first element you could use .Take(1) instead of First.

like image 77
Mark Byers Avatar answered Jul 31 '26 16:07

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!