Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select IDs where boolean property is true using linq

I want to select IDs from list where boolean IsActive= true using linq .
My Model class MyModel. I want to select IDs from list where IsActive is true.

        List<MyModel> ModelEntities = new List<MyModel>();
        MyModel Model = new MyModel();

        Model.IsActive=true;
        Model.ID = 1;
        ModelEntities.Add(Model);

        Model = new MyModel();
        Model.IsActive=true;
        Model.ID = 2;
        ModelEntities.Add(Model);

        
        Model = new MyModel();
        Model.IsActive=true;
        Model.ID = 3;         
        ModelEntities.Add(Model);

    
        Model = new MyModel();
        Model.IsActive=False;
        Model.ID = 4;         
        ModelEntities.Add(Model);

        
        Model = new MyModel();
        Model.IsActive=true;
        Model.ID = 5;         
        ModelEntities.Add(Model);

        
        Model = new MyModel();
        Model.IsActive=False;
        Model.ID = 6;         
        ModelEntities.Add(Model);

        
        Model = new MyModel();
        Model.IsActive=False;
        Model.ID = 7;         
        ModelEntities.Add(Model);

        
        Model = new MyModel();
        Model.IsActive=False;
        Model.ID = 8;         
        ModelEntities.Add(Model);

I want result like {1,2,3,5 }. How can get those IDs using Where condition in linq.

Edited
I just want list of IDs where IsActive = true .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

var ids = ModelEntities
            .Where(model => model.IsActive)
            .Select(model => model.ID)
            .ToList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading