【问题标题】:Compare two objects List<Items> in C# UWP在 C# UWP 中比较两个对象 List<Items>
【发布时间】:2016-12-29 11:47:46
【问题描述】:

我正在尝试根据更新时间(保存在数据库中)下载基于服务器的文件 (PDF)。在下载之前,我想将它们与本地机器上的现有文件列表进行比较。问题是比较对象字段给出了错误的输出。

这两个文件都是基于 json 的文件,在下面给出的“ITEMS”对象中解析。我正在使用带有 C# 的 Visual Studio 2015。后端是 Laravel REST。

class Items
{
    public int id { get; set; }
    public string branch { get; set; }
    public string item { get; set; }
    public string link { get; set; }
    public int active { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
}

这就是我解析服务器和本地列表(均为 JSON)的方式:

for (int i = 0; i < obj.Count; i++)
{
     JObject row = JObject.Parse(obj[i].ToString());
     Items newItem = new Items();
     newItem.id = int.Parse(row["id"].ToString());
     newItem.branch = row["branch"].ToString();
     newItem.item = row["item"].ToString();
     newItem.link = row["link"].ToString();
     newItem.created_at = row["created_at"].ToString();
     newItem.updated_at = row["updated_at"].ToString();
     files.Add(newItem);
}

我正在使用 foreach 循环来检查 updated_at 字段是否相等

// Compare files

foreach (Items item in files)
{  
    foreach(Items it in filesToDownload)
    {
        if (!it.updated_at.Equals(item.updated_at))
        {
        //Download the file
        //Create a new list of files to download
        }
    }
}

【问题讨论】:

    标签: c# json collections storagefile


    【解决方案1】:
    for (int i = 0; i < obj.Count; i++)
    {
        JObject row = JObject.Parse(obj[i].ToString());
        Items newItem = new Items();
        newItem.id = int.Parse(row["id"].ToString());
        newItem.item = row["branch"].ToString();
        newItem.link = row["item"].ToString();
        newItem.item = row["link"].ToString();
        newItem.item = row["created_at"].ToString();
        newItem.item = row["updated_at"].ToString();
        files.Add(newItem);
    }
    

    您正在为所有变量设置item 属性。

    试试:

    for (int i = 0; i < obj.Count; i++)
    {
        JObject row = JObject.Parse(obj[i].ToString());
        Items newItem = new Items();
        newItem.id = int.Parse(row["id"].ToString());
        newItem.branch = row["branch"].ToString();
        newItem.item = row["item"].ToString();
        newItem.link = row["link"].ToString();
        newItem.created_at = row["created_at"].ToString();
        newItem.updated_at = row["updated_at"].ToString();
        files.Add(newItem);
    }
    

    【讨论】:

      【解决方案2】:

      如果列表确实可比较并且它们具有完全相同的格式,例如 item.updated_at C# 提供了使用函数相交或除外来“本地”比较列表的可能性;

      请记住,该程序不会检查合理性,因此,如果您认为循环遍历数组时已经正确并且仍然得到错误的结果,那么如果这可能是问题所在,我将首先检查数据。

      因为看起来 foreach 循环会像 Intersect 和 except 一样做。

      首先调试循环,然后使用 Expression-Checker(或像 OzCode 这样的简单工具)自行比较列表。

      我也喜欢比较 Id 而不是使用时间戳。如果它们兼容。

      两个有用的链接:

      Intersect Two Lists in C#

      The opposite of Intersect()

      【讨论】:

      • 这些 lambda 表达式对我来说太复杂了。所以我尝试做foreach循环。感谢您的帮助。
      【解决方案3】:

      我认为你只是复制+粘贴太多... =)

      for (int i = 0; i < obj.Count; i++)
      {
           JObject row = JObject.Parse(obj[i].ToString());
           Items newItem = new Items();
           newItem.id = int.Parse(row["id"].ToString());
           newItem.item = row["branch"].ToString();
           newItem.link = row["item"].ToString();
           newItem.item = row["link"].ToString();
           newItem.item = row["created_at"].ToString();
           newItem.item = row["updated_at"].ToString();
           files.Add(newItem);
      }
      

      您多次重复newItem.item

      【讨论】:

      • 谢谢。已更正。复制到 stackoverflow 时出错。
      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 2018-02-26
      • 2023-03-07
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多