Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make this class an IEnumerable?

Tags:

c#

asp.net-mvc

I created the following view model for an MVC application. What I'd like to do is make it an IEnumerable class so that I can iterate through the data in my page using a foreach statement.

public class EstimateDetailsModel
{
    public string dma { get; set; }
    public string callsign { get; set; }
    public string description { get; set; }

}

In case it's relevant, here is the corresponding Linq query in my repository that instantiates the EstimatesDetailsModel class:

public IEnumerable<EstimateDetailsModel> GetEstimateDetails(int id)
{
    var estimateDetails = from e in db.Estimates
                          join es in db.EstimateStations on e.EstimateID equals es.EstimateID
                          join s in db.Stations on es.StationID equals s.StationID
                          join m in db.Markets on s.MarketID equals m.MarketID
                          where e.EstimateID == 1
                          select new EstimateDetailsModel { dma = m.DmaName, callsign = s.CallSign, description = s.StationDescription };
    return estimateDetails;                                  
}
like image 320
hughesdan Avatar asked Jan 18 '12 01:01

hughesdan


1 Answers

Perhaps you are looking for something like this:

public class EstimateDetailsModel : IEnumerable<string>
{
    public string Dma { get; set; }

    public string Callsign { get; set; }

    public string Description { get; set; }

    public IEnumerator<string> GetEnumerator()
    {
        yield return Dma;
        yield return Callsign;
        yield return Description;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

(I have capitalised the properties, which is the normal style.)

like image 90
Matthew Strawbridge Avatar answered Sep 20 '22 21:09

Matthew Strawbridge