简体   繁体   English

从 C# 对象列表中获取特定对象的值

[英]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.我正在一个 WPF 窗口中工作,在该窗口中我有一个使用 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.我的查询是从上述列表中获取EmployeeLastName 中存储的所有值,并将每个值与 TextBox txtEmpLastName的值进行比较,并在发现 else FALSE 时返回 TRUE。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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