如何解决迭代不能对“getenumerator”的公共定义类型的变量进行操作
我正在做一个搜索,其中我正在调用 API 并将 XML 响应以及 SerializeXmlNode 和 DeserializeObject 获取到我的根对象。现在的问题是当我尝试使用 foreach 进行循环时。
我在下面收到此错误:
foreach 语句不能对类型 (Model.AccountLite) 的变量进行操作,因为不包含“getenumerator”的公共实例定义
我已经检查了这个 data = JsonConvert.DeserializeObject(json);,我可以看到数据。
我曾尝试查看此 previously asked question
public static List<AccountLite> searchAccounts(string searchString)
{
List<AccountLite> result = new List<AccountLite>();
Root data = new Root();
string[] contains = searchString.Split(' ');
RestClient client = new RestClient(baseUrl);
foreach (string contain in contains)
{
if (contain.Length < 3) continue;
RestRequest request = new RestRequest($"/xx/xx/xx/xxx/xxx/account?xx=Lite&searchString={searchString}");
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.AddHeader("Authorization","Basic " + encoded);
IRestResponse response = client.Execute(request);
string requestResponse = response.Content;
//Converting data from XML into Json and deserializet json object
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(requestResponse);
string json = JsonConvert.SerializeXmlNode(doc);
data = JsonConvert.DeserializeObject<Root>(json);
}
catch (Exception)
{
continue;
}
if (data?.SiebelMessageEnvelope?.listofAccountLite?.AccountLite == null)
continue;
//this line is the one showing error.
foreach (AccountLite item in data.SiebelMessageEnvelope.listofAccountLite.AccountLite)
{
bool containsBoth = true;
foreach (string contain2 in contains)
{
if (!item.Name.ToLower().Contains(contain2.ToLower()) && !item.Id.ToLower().Contains(contain2.ToLower()))
containsBoth = false;
}
if (containsBoth)
{
if (result.FirstOrDefault(i => i.Id == item.Id) == null)
{
result.Add(item);
}
}
}
}
return result;
}
型号
public class AccountLite
{
public string Id { get; set; }
public string AccountStatus { get; set; }
public string AccountTypeCode { get; set; }
public string Location { get; set; }
public string Name { get; set; }
public string SRIntegrationFlag { get; set; }
}
public class listofAccountLite
{
public AccountLite AccountLite { get; set; }
}
public class SiebelMessageEnvelope
{
[JsonProperty("@xmlns")]
public string Xmlns { get; set; }
public listofAccountLite listofAccountLite { get; set; }
}
public class Root
{
public SiebelMessageEnvelope SiebelMessageEnvelope { get; set; }
}
Json 对象
{
"SiebelMessageEnvelope":{
"@xmlns":"","listofAccountLite":{
"AccountLite":{
"Id":"","AccountStatus":"","AccountTypeCode":"","Location":"","Name":"","SRIntegrationFlag":""
}
}
}
}
解决方法
您的 ListOfAccountLite
只包含一个 AccountLite
。在单个对象上 foreach
没有意义,其中该对象不可枚举(意思是:实现了 IEnumerable[<T>]
或包含显式 GetEnumerator()
方法)。
只有一个对象,所以……拿走它。而不是
if (data?.SiebelMessageEnvelope?.ListOfAccountLite?.AccountLite == null)
continue;
foreach (AccountLite item in data.SiebelMessageEnvelope.ListOfAccountLite.AccountLite)
{
// ... do the thing
}
简单
var item = data?.SiebelMessageEnvelope?.ListOfAccountLite?.AccountLite;
if (item is null)
continue;
// ... do the thing
也就是说:您可能应该调查 JSON 等中的 ListOfAccountLite
是否意味着 数组 而不是单个对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。