Compare two objects List<Items> in C# UWP

395 views Asked by At

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

There are 3 answers

0
Nathangrad On BEST ANSWER
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);
}
1
Anderson Pimentel On

I think you just copy+pasted too much... =)

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 repeated newItem.item several times.

1
gunderson On

If the lists are really compareable and they have exactly the same format, for say item.updated_at C# gives the possibility to compare lists "natively" using the Functions Intersect or Except;

Keep in Mind that the program won't check the plausbility, so if you think you got it right already with looping through the arrays and still get wrong results, I would start with checking the data if this could be the problem.

Because it seems like the foreach loops would do the same like Intersect and Except do.

Start by Debugging the loops and use the Expression-Checker (or an easy one like OzCode) to compare the lists by yourself.

Also I'm a fan of comparing Id's instead of using the timestamp. If they are compatible.

Two helpful links:

Intersect Two Lists in C#

The opposite of Intersect()