Filtering list objects from another list

I have the following class in my C # .NET 3.5 win forms application:

class Field {

string objectName;
string objectType;
string fieldName;
string fieldValue;


}

and ListList List, which is the data source for the marked list. This list shows all the individual object names from my fieldList collection.

I want to create another checklistbox that contains field names, but only shows the names of the fields that have the associated verified name of the object in the first list.

So my question is, how can I query the data source of the original list of object names to return a separate set of field names associated with the selected objectName?

This is not very easy to read, so I will give an example:

Field1 {

objectName = 'objA'
FieldName = 'FieldA'

}

Field2 {

objectName = 'objA'
FieldName = 'FieldB'

}


Field3 {

objectName = 'objB'
FieldName = 'FieldA'

}

Field4 {

objectName = 'objC'
FieldName = 'FieldC'

}

So, suppose in my checkbox I select objectNames objA and objB. Then my returned fields will be "FieldA" and "FieldB".

LINQ ? "select" "where", ?

+3
2
var selectedNames = ... // List of selected names
var selectedFields = (from f in fieldList
                      where selectedNames.Contains(f.objectName)
                      select f.FieldName).Distinct().ToList();
+1

; . - :

    string[] objectNames = { "objA", "objC" };
    var hashSet = new HashSet<string>(objectNames);

    var qry = (from row in data
               where hashSet.Contains(row.objectName)
               select row.fieldName).Distinct().ToList();

()

(, ), ():

    var selectedNames = namesCheckedListBox.CheckedItems.Cast<Field>()
        .Select(field => field.objectName);
    var hashSet = new HashSet<string>(selectedNames);

( Distinct() , HashSet<T> )

+2

Source: https://habr.com/ru/post/1702175/


All Articles