How to look for a specific value inside a list object

PratikShah111
PratikShah111 used Ask the Experts™
on
I have a variable defined as

var model = new List<someviewmodel>();

if i drill down model

model[0].items[0].inspectionitems[0].Value = 62
or
model[0].items[0].inspectionitems[0].Value = 63

And then i have another variable with just a number in it

var number = 62

Another property I have inside the model is "isSelected".

what I basically want to do is whatever value i have in var number, I want to go in Model and find that particular item and set the "isSelected" property to true.

C#
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. Software Engineer
Commented:
you need to use nested loops like this:

var modelList =  new List<someviewmodel>();
var number = 62;
foreach(var model in modelList){
    foreach(var item in model.Items){
        foreach(var inspectionItem in item.inspectionItems){
            if(inspectionItem.Value == number){
                inspectionItem.isSelected = true;
            }
        }
    }
}

Open in new window

Commented:
You could also use LINQ:
var models = new List<someviewmodel>();
var number = 62;
foreach (var inspectionItem in (from model in models
								from item in model.Items
								from inspectionItem in item.inspectionItems
								where inspectionItem.Value.Equals(number)
								select inspectionItem))
{
	inspectionItem.isSelected = true;
}

Open in new window

-saige-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial