如何使此类成为IEnumerable? [英] How do I make this class an IEnumerable?

查看:78
本文介绍了如何使此类成为IEnumerable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为MVC应用程序创建了以下视图模型.我想做的是将其设置为IEnumerable类,以便可以使用foreach语句遍历页面中的数据.

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; }

}

在相关的情况下,这是我的资源库中实例化EstimatesDetailsModel类的相应Linq查询:

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;                                  
}

推荐答案

也许您正在寻找这样的东西:

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.)

这篇关于如何使此类成为IEnumerable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆