S
S
Stepan Fedoseev2021-12-20 20:32:02
C++ / C#
Stepan Fedoseev, 2021-12-20 20:32:02

How to find an element in a List collection by a specific field of an object?

Good day. There are two collections. It is necessary to sequentially search for matches of a number of fields of objects from one list in another, performing an action after each operation. The field objects are not identical, so the comparison must be made across a number of fields.

Example of non-working code below =)

static (List<FileInfo> coincidenc, List<FileInfo> uniqueValuesDir1, List<FileInfo> uniqueValuesDir2) FilesChecker(FileInfo[] dir1, FileInfo[] dir2)
{
    List<FileInfo> coincidenc = new();
    List<FileInfo> uniqueValuesDir1 = dir1.ToList();
    List<FileInfo> uniqueValuesDir2 = dir2.ToList();

    foreach (var item in uniqueValuesDir2)
    {
        if (uniqueValuesDir1.Find(x => x.Name == item.Name)) != -1) 
        {
            coincidenc.Add(item);
            uniqueValuesDir1.Remove(item);
            uniqueValuesDir2.Remove(item);
        }
    }
    return (coincidenc, uniqueValuesDir1, uniqueValuesDir2);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question