我有以下课程

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的新手,谁能告诉我我需要做些什么才能使我能够进行前言?

谢谢

最佳答案

最简单的方法就是枚举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)
{
    ...
}

关于c# - 使类成为IEnumerable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16261454/

10-12 06:05