简体   繁体   中英

Get the values of a particular object from a C# List of objects

I'm working in a WPF window in which I have a class which is fetching data from the database using dbset. Below is the view model class of data.

public class CriteriaSheetVM
    {
        public string ReviewNumber { get; set; }

        public string EmployeeFirstName { get; set; }

        public string EmployeeLastName { get; set; }
    }

and i'm creating a list of above class objects:

List<CriteriaSheetVM> criteriaSheet;

My query is to get all the values stored in the EmployeeLastName from the above List and to compare each value with the value in the TextBox txtEmpLastName and return TRUE when found else FALSE.

UPDATE:

I have used below code for it.

criteriaSheet.ForEach(a =>
                {
                    if (a.EmployeeLastName == txtEmpLastName.Text)
                    {
                       bool flag = 1;
                    }
                });

and it was not working

If you want multiple result:

var results = criteriaSheet.Where(cs=>cs.EmployeeLastName ==txtEmpLastName.Text);

If you want to one result:

var result = criteriaSheet.FirstOrDefault(cs=>cs.EmployeeLastName ==txtEmpLastName.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM