views:

83

answers:

5

I've got 2 generic lists that do not contain the all fields of the same type

IList<Category> and List<CategoriesRow>Categories

categoryList = IList<Category>

but both have common fields Name and ID.

I want to compare list Categories with categoryList and find from categoryList where categoryList[index].ID does not exist in the list of all Categories of ID. For all those that do not exist in Categories, I will have to remove them from CatgoryList.

I had a previous post in which I was given examples of LINQ, but the problem is I have to use Dynamic, implying that I am passing categoryList and Categories as Dynamic.

Can anyone give me an example how to go about the above as I have no idea how to do it.

A: 

First run a loop of all list element of CategoryList
Run the inner loop of all list element of Categories
Compare the id of CategoryList[index].ID with Categories[InnerIndex].ID
When the inner loop is finish, check if the condition is ever true in whole loop if so then the ID is exists in Categories otherwise not.

Himadri
A: 
Jeff M
Those two lists are not of the same type, so it won't work.
Jaroslav Jandek
I had just realized that after I posted, jumped the gun there.
Jeff M
A: 
for (int i = categoryList.Count-1; i >= 0; i--)
{
  int id = categoryList[i].ID;
  if (!Categories.Any(r => r.ID == id)) categoryList.RemoveAt(i);
}
Jaroslav Jandek
I am using dynamic, hence can`t use LINQ or lambda
Why not? I can use such code even if either `CategoriesRow` or `Category` is dynamic. Are you using some beta version of .NET4? Do you get an exception? If so, post it.
Jaroslav Jandek
A: 

This might help you

for (int i = 0; i < CategoryList.Count; i++) { 
        if (!CategoryList.Contains(Categories[i]))
            Categories.RemoveAt(i);
        if (!Categories.Contains(CategoryList[i]))
            CategoryList.RemoveAt(i);
    }
Gunner 4 Life
I am having the error message Error 4 Argument 1: cannot convert from 'API.Category to 'MvcUI.Models.CategoryRow' \MvcUI\Models\ProjectModel.cs 330 55 MvcUICategory is a class and CatregoryRow is another class, which i`m using for the list<object>
@Gunner 4 Life: That can't work since the two lists are not of the same type. @user281180: my answer does *exactly* what you asked for (removes objects that have no `ID` in `Categories` from `categoryList`).
Jaroslav Jandek
@Jaroslav thanks for answering. I am using dynamic and due to that I can`t use lambda, hence can`t apply it! I can`t use LINQ in this case.
A: 
public class Category
{
    public int Id;
}

public class CategoriesRow
{
    public int Id;
}

public List<Category> RemoveNotUsedCategories(List<Category> categoryList, List<CategoriesRow> Categories)
{
    List<Category> usedCategories = new List<Category>();

    foreach (Category usableCat in categoryList)
    {
        foreach (CategoriesRow catRow in Categories)
        {
            if (usableCat.Id == catRow.Id)
            {
                // Add categories that are used.
                usedCategories.Add(usableCat);
                // Will break the first loop.
                break;
            }
        }
    }
    return usedCategories;
}
MrFox