Roman Ratskey
Roman Ratskey

Reputation: 5269

Get element from a List<Class> of classes

I have a class called Employee that represents each employee in a company.

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }
}

I have created a new List of Employees

public readonly List<Employee> Employees = new List<Employee>();

Now i want to find an employee from the list by his\her name but i don't know how to achieve that so your help will be very appreciated and thanks.

Upvotes: 0

Views: 360

Answers (11)

timthacker63
timthacker63

Reputation: 51

If you need to wild card search use contains.

Employees.Where(emp => emp.FirstName.Contains("Rune") && emp.LastName.Contains("S"));

Upvotes: 0

Ilya Ivanov
Ilya Ivanov

Reputation: 23646

Try to use next code snippet to

var firstName = "Jon";
var lastName = "Skeet";
var employees = Employees.Where(emp => emp.FirstName == firstName && emp.LastName == lastName);

If you want to find exactly one employee, then you probably want to use Single instead of Where, which in my case is more plausible. In case if you want to find the first employee, that fits that criteria, use 'First` instead of 'Where'.

Upvotes: 1

jevakallio
jevakallio

Reputation: 35970

Using a LINQ-query:

Employees.FirstOrDefault(emp => emp.FirstName == "Rune" && emp.LastName == "S");

This will return you the first employee by that name, or if none are found, null. To get all employees with the name, use Where instead:

Employees.Where(emp => emp.FirstName == "Rune" && emp.LastName == "S");

Upvotes: 3

Jacco
Jacco

Reputation: 3272

The best way to do this is using LINQ:

var employees = Employees.Where(e => e.FirstName.Equals("RuneS")).ToArray();

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460380

The non-Linq approach using List.FindAll

List<Employee> foundEmployees = Employees
    .FindAll(e => e.LastName.Equals(empName, StringComparison.OrdinalIgnoreCase));

Used the Equals approach to show you how to search ignoring the case.

Upvotes: 2

Shawn
Shawn

Reputation: 9472

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Check out the example for WHERE simple1 and simple2.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

You can use a loop, or use a LINQ:

This expression gives you the first employee with the last name "Smith", or null if nobody by this last name is found on your list:

var res = Employees.FirstOrDefault(e => e.LastName  == "Smith");

This loop lets you enumerate all employees with the first name "John":

foreach (var employee in Employees.Where(e => e.FirstName  == "John")) {
    ...
}

You can also make a list of employees passing a given filter:

var smiths = Employees.Where(e => e.LastName  == "Smith").ToList();

If the original list contains no "Smith"s, the resulting list would be empty.

If you must not use LINQ, you can use a plain foreach loop:

foreach (var employee in Employees) {
    if (employee.LastName == "Smith") {
        ...
    }
}

Upvotes: 1

David
David

Reputation: 219127

When you say "find by his/her name" do you mean that you're looking for all of the people named "John" for example? Using LINQ, that would look like this:

var johns = Employees.Where(e => e.FirstName == "John");

Keep in mind that this returns an IEnumerable<Employee> instead of an IList<Employee>. There are a number of differences between the two.

.Where() is one of many extension methods that you can use to query and manipulate your lists.

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

This will give you a IEnumerable<Employee> of employees with matched first and last name:

string firstName = "Bob";
string LastName = "Smith";
var employees = Employees.Where(e => e.FirstName == firstName && 
                                     e.LastName == lastName);

Or with query syntax:

var employees = from e in Employees
                where e.FirstName == firstName && 
                      e.LastName == lastName
                select e;

If there should be only one employee matching that criteria, then use SingleOrDefault:

Employee employee = Employees.SignleOrDefault(e => e.FirstName == firstName && 
                                                   e.LastName == lastName);

Upvotes: 1

Gabe
Gabe

Reputation: 50533

Just use LINQ

var employee = Employees.Where(e => e.LastName == lastname).FirstOrDefault();

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245509

If you're able to use LINQ, you could simply query the List:

Employees.Where(e => e.FirstName == "John" || e.LastName == "Doe");

Or, you could also quite easily loop through the list and add any matches to another resulting list:

List<Employee> matches = new List<Employee>();

foreach(var employee in Employees)
{
    if(employee.FirstName == "John" || employee.LastName == "Doe")
        matches.Add(employee);
}

Upvotes: 1

Related Questions