Search code examples
c#jsoncollectionsstoragefile

Compare two objects List<Items> in C# UWP


I am trying to download server based files (PDF) depending on their update time (Saved in database). Before download, I want to compare them with existing list of files on local machine. Problem is comparing the object fields gives wrong output.

Both files are json based files, parsed in below given "ITEMS" object. I am using Visual Studio 2015 with C#. Backend is 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; }
}

This is how I am parsing the server and local list (both 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);
}

I am using foreach loop to check if the updated_at field is equal or not

// 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
        }
    }
}

Solution

  • 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);
    }
    

    You're setting the item property for all the variables.

    Try:

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