gpt4 book ai didi

c# - 使类 IEnumerable

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:20 27 4
gpt4 key购买 nike

我有以下类(class)

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 View 中返回的模型中。

但是我得到了错误:

foreach statement cannot operate on variables of type 'Taxi.Model.SearchResult' because 'Taxi.Model.SearchResult' does not contain a public definition for 'GetEnumerator'

我是 c#.net 的新手,谁能告诉我需要做什么才能让我执行 foreach?

谢谢

最佳答案

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

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

但要能够枚举 SearchResultOffer 本身,您必须实现 IEnumerable 接口(interface):

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com