比较 C# UWP 中的两个对象 List<Items>
Compare two objects List<Items> in C# UWP
我正在尝试根据更新时间(保存在数据库中)下载基于服务器的文件 (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
}
}
}
我觉得你复制+粘贴的太多了... =)
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
几次。
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);
}
如果列表确实可以比较并且它们具有完全相同的格式,例如 item.updated_at C# 可以使用相交或除外函数比较列表 "natively";
请记住,该程序不会检查合理性,因此如果您认为您已经通过循环遍历数组得到了正确的结果但仍然得到错误的结果,那么我会从检查数据开始,如果这可能是问题。
因为看起来 foreach 循环会像 Intersect 和 Except 那样做。
首先调试循环并使用 Expression-Checker(或像 OzCode 这样的简单工具)自己比较列表。
我也喜欢比较 Id 而不是使用时间戳。如果兼容。
两个有用的链接:
Intersect Two Lists in C#
The opposite of Intersect()
我正在尝试根据更新时间(保存在数据库中)下载基于服务器的文件 (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
}
}
}
我觉得你复制+粘贴的太多了... =)
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
几次。
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);
}
如果列表确实可以比较并且它们具有完全相同的格式,例如 item.updated_at C# 可以使用相交或除外函数比较列表 "natively";
请记住,该程序不会检查合理性,因此如果您认为您已经通过循环遍历数组得到了正确的结果但仍然得到错误的结果,那么我会从检查数据开始,如果这可能是问题。
因为看起来 foreach 循环会像 Intersect 和 Except 那样做。
首先调试循环并使用 Expression-Checker(或像 OzCode 这样的简单工具)自己比较列表。
我也喜欢比较 Id 而不是使用时间戳。如果兼容。
两个有用的链接:
Intersect Two Lists in C#
The opposite of Intersect()