[英]Compare two objects List<Items> in C# UWP
我正在嘗試根據服務器的更新時間(基於數據庫的保存)下載基於服務器的文件(PDF)。 在下載之前,我想將它們與本地計算機上的現有文件列表進行比較。 問題是比較對象字段給出錯誤的輸出。
這兩個文件都是基於json的文件,在下面的“ ITEMS”對象中進行了解析。 我正在使用Visual Studio 2015和C#。 后端是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);
}
您正在為所有變量設置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#可以使用“函數相交”或“除”來“本地”比較列表;
請記住,該程序不會檢查真實性,因此如果您認為通過遍歷數組已經正確並仍然得到錯誤的結果,那么我將從檢查數據開始,如果這可能是問題所在。
因為看起來foreach循環會像Intersect和Except一樣做。
首先調試循環,然后使用Expression-Checker(或諸如OzCode這樣的簡單程序)自己比較列表。
另外,我還是比較ID而不是使用時間戳的粉絲。 如果它們兼容。
兩個有用的鏈接:
我認為您只是復制+粘貼過多... =)
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
幾次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.