制作一个IEnumerable类

时间:2013-04-28 09:26:02

标签: c# asp.net-mvc visual-studio class ienumerable

我有以下课程

namespace TaxiResults.Model
{

public class SearchResult
{
    public string status { get; set; }
    public SearchResultResult result { get; set; }
}

public class SearchResultResult
{
    public SearchResultOffer offer { get; set; }
}

public class SearchResultOffer
{
    public SearchResultOffer1[] offers { get; set; }
}

public class SearchResultOffer1
{
    public SearchResultAddress[] addresses { get; set; }
    public string chnl { get; set; }
    public string companyInfoId { get; set; }
    public string companyName { get; set; }
    public string bookingId { get; set; }
    public string tz { get; set; }
    public string pickupDateTime { get; set; }
    public string pickupDateTimeUTC { get; set; }
    public int routeDuration { get; set; }
    public int routeDistance { get; set; }
    public SearchResultPricedetail priceDetail { get; set; }
    public float tax { get; set; }
    public string services { get; set; }
    public int bagCount { get; set; }
    public int passengerCount { get; set; }
    public int productType { get; set; }
    public string resultset { get; set; }
    public string companyInfoId3cd { get; set; }
}

public class SearchResultPricedetail
{
    public SearchResultOriginalprice originalPrice { get; set; }
    public SearchResultFinalprice finalPrice { get; set; }
}

public class SearchResultOriginalprice
{
    public SearchResultAmount[] amounts { get; set; }
}

public class SearchResultAmount
{
    public string currency { get; set; }
    public string priceExcludingTax { get; set; }
    public float priceExcludingTaxNumber { get; set; }
    public string priceTotal { get; set; }
    public float priceTotalNumber { get; set; }
    public float exchangeRate { get; set; }
    public string priceTax { get; set; }
    public float priceTaxNumber { get; set; }
}

public class SearchResultFinalprice
{
    public SearchResultAmount1[] amounts { get; set; }
}

public class SearchResultAmount1
{
    public string currency { get; set; }
    public string priceExcludingTax { get; set; }
    public float priceExcludingTaxNumber { get; set; }
    public double priceTotal { get; set; }
    public float priceTotalNumber { get; set; }
    public float exchangeRate { get; set; }
    public string priceTax { get; set; }
    public float priceTaxNumber { get; set; }
}

public class SearchResultAddress
{
    public SearchResultLocation location { get; set; }
    public string address { get; set; }
    public int type { get; set; }
}

public class SearchResultLocation
{
    public float lat { get; set; }
    public float lng { get; set; }
}

}

我想在Taxi.Model.SearchResult上做一个foreach循环,将它提供给我在MVC视图中返回的模型。

但是我收到了错误:

  

foreach语句无法对“Taxi.Model.SearchResult”类型的变量进行操作,因为“Taxi.Model.SearchResult”不包含“GetEnumerator”的公共定义

我是c#.net的新手可以有人告诉我我需要做些什么来让我做一个foreach吗?

由于

3 个答案:

答案 0 :(得分:3)

最简单的方法就是枚举offers属性,如下所示:

SearchResultOffer searchResults = ...
foreach(var result in searchResults.offers)
{
    ...
}

但是为了能够枚举SearchResultOffer本身,你必须实现IEnumerable接口:

public class SearchResultOffer : IEnumerable<SearchResultOffer1>
{
    public SearchResultOffer1[] offers { get; set; }

    // Generic version for IEnumerable<T>
    public IEnumerator<SearchResultOffer1> GetEnumerator() 
    {
        return offers.Cast<SearchResultOffer1>().GetEnumerator();
    }

    // Non-generic version for IEnumerable
    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator(); // calls the generic version
    }
}

然后你可以这样做:

SearchResultOffer searchResults = ...
foreach(var result in searchResults)
{
    ...
}

答案 1 :(得分:1)

查看您提供的代码,看起来SearchResult类实际上是底层实体的单个实例,而不是集合。您可能需要声明List<SearchResult>来存储所有结果,然后在该列表中使用foreach

答案 2 :(得分:0)

您应该实施IEnumerable<T>IEnumerable来支持foreach。