[LINQ Operators] Sample Data Objects for Examples

I am going to post all LINQ operators one by one with examples. Almost all examples will use the following 6 classes.

  • Employee
  • City
  • Department
  • EmployeeDepartment
  • DepartmentEmployees
  • Food

The “DepartmentEmployees” class mimics the one-to-many relationship query result to explain some query operators (such as “SelectMany”) more easily.         

 

1. Employee

public class Employee
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int CityId { get; set; }

  public static List<Employee> GetEmployees()
  {
    return new List<Employee>
    {
      new Employee { Id=1, Name="Tom Harris", CityId=1 },
      new Employee { Id=2, Name="Jane Smith", CityId=2 },
      new Employee { Id=3, Name="John King", CityId=2 },
      new Employee { Id=4, Name="Mark Smith", CityId=1 },
      new Employee { Id=5, Name="Christy Baker", CityId=1 }
    };
  }
}

 

2. City

public class City
{
  public int Id { get; set; }
  public string Name { get; set; }

  public static List<City> GetCities()
  {
    return new List<City>
    {
      new City { Id=1, Name="New York" },
      new City { Id=2, Name="Seattle" }
    };
  }
}

 

3. Department

public class Department
{
  public int Id { get; set; }
  public string Name { get; set; }

  public static List<Department> GetDepartments()
  {
    return new List<Department>
    {
      new Department { Id=1, Name="Sales" },
      new Department { Id=2, Name="Engineering" }
    };
  }
}

 

4. EmployeeDepartment

public class EmployeeDepartment
{
  public int EmployeeId { get; set; }
  public int DepartmentId { get; set; }

  public static List<EmployeeDepartment> GetEmployeeDepartmentMapping()
  {
    return new List<EmployeeDepartment>
    {
      new EmployeeDepartment { EmployeeId=1, DepartmentId=1 },
      new EmployeeDepartment { EmployeeId=2, DepartmentId=1 },
      new EmployeeDepartment { EmployeeId=3, DepartmentId=1 },
      new EmployeeDepartment { EmployeeId=4, DepartmentId=2 },
      new EmployeeDepartment { EmployeeId=5, DepartmentId=2 }
    };
  }
}

 

5. DepartmentEmployees

public class DepartmentEmployees
{
  public Department Department { get; set; }
  public List<Employee> Employees { get; set; }

  public static List<DepartmentEmployees> GetDepartmentEmployees()
  {
    List<DepartmentEmployees> mappings = new List<DepartmentEmployees>();

    DepartmentEmployees mapping1 = new DepartmentEmployees
    {
      Department = new Department { Id = 1, Name = "Sales" },
      Employees = new List<Employee>
      {
        new Employee { Id=1, Name="Tom Harris", CityId= 1 },
        new Employee { Id=2, Name="Jane Smith", CityId= 2 },
        new Employee { Id=3, Name="John King", CityId= 2 }
      }
    };

    DepartmentEmployees mapping2 = new DepartmentEmployees
    {
       Department = new Department { Id = 2, Name = "Engineering" },
       Employees = new List<Employee>
       {
         new Employee { Id=4, Name="Mark Smith", CityId= 1 },
         new Employee { Id=5, Name="Christy Baker", CityId= 1 }
       }
    };
    mappings.Add(mapping1);
    mappings.Add(mapping2);

    return mappings;
  }
}

 

6. Food

public class Food
{
  public string Name { get; set; }
  public decimal? Price { get; set; }

  public static List<Food> GetFoods()
  {
    return new List<Food>
    {
      new Food { Name="Bread", Price=3.99M },
      new Food { Name="Coffee", Price=1.99M },
      new Food { Name="Burger", Price=4.99M },
      new Food { Name="Coke", Price=1.25M },
      new Food { Name="Salad", Price=6.50M },
      new Food { Name="Pizza", Price=8.99M },
      new Food { Name="Special", Price=null }
    };
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s