2016-12-29 99 views
0

我試圖根據更新時間(保存在數據庫中)下載基於服務器的文件(PDF)。在下載之前,我想將它們與本地機器上現有的文件列表進行比較。問題是比較對象字段給出錯誤的輸出。比較兩個對象列表中的<Items>在C#UWP

這兩個文件都是基於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 
     } 
    } 
} 

回答

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); 
} 
+0

是的。糾正。 –

0

我認爲你只是複製+粘貼太多... =)

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幾次。

+1

謝謝。糾正。複製到stackoverflow時是錯誤的。 –

1

如果名單真的compareable,他們具有完全相同的格式,說item.updated_at C#提供了使用功能交叉或除「原生地」比較清單的可能性;

請記住,程序不會檢查可信度,所以如果你認爲你已經正確地通過數組循環並且仍然得到錯誤的結果,我會開始檢查數據,如果這可能是問題。

因爲它看起來像foreach循環會像Intersect和Except一樣做。

首先調試循環並使用Expression-Checker(或簡單的OzCode)來自行比較列表。

此外,我是比較Id的,而不是使用時間戳的粉絲。如果它們兼容。

兩個有用的鏈接:

Intersect Two Lists in C#

The opposite of Intersect()

+0

這些lambda表達式對我來說太複雜了。所以我嘗試做foreach循環。謝謝你的幫助。 –

+0

你能幫我寫出那段代碼嗎? –