2016-12-29 9 views
0

更新時間(データベースに保存されている)に応じて、サーバーベースのファイル(PDF)をダウンロードしようとしています。ダウンロードする前に、ローカルマシン上の既存のファイルリストと比較したいと思います。問題はオブジェクトフィールドを比較している間違った出力を与えます。2つのオブジェクトを比較する<Items> C#UWP

両方のファイルは、以下の "ITEMS"オブジェクトで解析されたjsonベースのファイルです。私は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); 
} 

私は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 
     } 
    } 
} 
であるかどうかを確認するために、foreachループを使用しています

答えて

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 doのように同じことをするでしょう。

開始ループをデバッグし、Expression Checker(またはOzCodeのような簡単なもの)を使用してリストを自分で比較します。

また、タイムスタンプを使用する代わりにIDを比較するファンです。互換性がある場合。

二役立つリンク:

Intersect Two Lists in C#

The opposite of Intersect()

+0

これらのラムダ式はあまりにも複雑です。だから私はforeachループをやろうとします。ご協力いただきありがとうございます。 –

+0

あなたはそのコードを書いてもらえますか? –

関連する問題