迭代不能对“getenumerator”的公共定义类型的变量进行操作

问题描述

我正在做一个搜索,其中我正在调用 API 并将 XML 响应以及 SerializeXmlNode 和 DeserializeObject 获取到我的根对象。现在的问题是当我尝试使用 foreach 进行循环时。

我在下面收到此错误

foreach 语句不能对类型 (Model.AccountLite) 的变量进行操作,因为不包含“getenumerator”的公共实例定义

我已经检查了这个 data = JsonConvert.DeserializeObject(json);,我可以看到数据。

我曾尝试查看此 previously asked question

搜索 API 调用

    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 是否意味着 数组 而不是单个对象。