C # list <T> get return sorted list

I have basically:

public class Object{
    public bool IsObjectValid { set; get; }
}

public class MyThing{
    public List<Object> Objects { set; get; }
}

What I want to do:

public class ObjectsFiltered{
    public List<Object> ValidObjects{
        get{
            var list = LFs.Sort<_LF> where (IsObjectValid == true);
            return list;
        }
    }
}

I know there should be a way to sort the list by filtering out bool true / false. I just can't completely wrap my head around Link. I just can't find a tutorial that screams "AH HA!". About Linq Lambda for me: /

I would rather just return a subset, only save only one “object” ... instead of the current setting of multiple sets of lists. KISS.

Ultimately, I will use bool-toggles to combine TreeViews in my WPF form.

: , , ( ) , . , ObjecstValid, ObjectsInvalid, ObjectsSomeOtherRuleSet... ...

, ... , , .

+3
4

LINQ:

public IEnumerable<Object> ValidObjects{ 
    get{ 
        return LFs.Where(item => item.IsObjectValid)
                  .OrderBy(item => item.SomeProperty); 
    } 
} 

List<T>, IEnumerable<T>, .

- item => item.SomeProperty , item item.SomeProperty. ( )

+4

, LINQ.

+1

To filter your objects, you can simply return:

return LFs.Where(x => x.IsObjectValid).ToList();

Please note, however, that if you intend to use this feature frequently, you can see a performance improvement by maintaining a pre-filtered list inside.

0
source
 LFs.Where(x => x.IsObjectValid).ToList().Sort()

Sort by default. Otherwise

 LFs.Where(x => x.IsObjectValid).OrderBy(x => x.PropertyToSortBy).ToList(); 
0
source

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


All Articles