1
\$\begingroup\$

I have a data set.

public class EmployeeRepository
{
    public static List<Employee> AllEmployees()
    {
        var loc1 = new List<Location> { new Location { City = "Gretna", State = "VA", ZipCode = "24587" }, new Location { City = "Charlotte", State = "NC", ZipCode = "28012" } };
        var loc2 = new List<Location> { new Location { City = "Boston", State = "MA", ZipCode = "02333" }, new Location { City = "Waldham", State = "MA", ZipCode = "02323" } };
        var loc3 = new List<Location> { new Location { City = "Greenville", State = "NC", ZipCode = "29011" }};
        var loc4 = new List<Location> { new Location { City = "Greenville", State = "SC", ZipCode = "26000" }, new Location { City = "Charleston", State = "SC", ZipCode = "26011" } };

        return new List<Employee>()
        {
            new Employee {Id = 1, Department = new Department{Id = 1, Name = "Technology" },  Locations = loc1 },
            new Employee {Id = 2, Department = new Department{Id = 1, Name = "Technology" },  Locations = loc2},
            new Employee {Id = 3, Department = new Department{Id = 1, Name = "Technology" },  Locations = loc3 },
            new Employee {Id = 4, Department = new Department{Id = 2, Name = "IT" },  Locations = loc4 },
            new Employee {Id = 5, Department = new Department{Id = 2, Name = "IT" },  Locations = loc4 },
            new Employee {Id = 6, Department = new Department{Id = 3, Name = "Sales" },  Locations = loc3 },
            new Employee {Id = 7, Department = new Department{Id = 3, Name = "Sales" },  Locations = loc2},
            new Employee {Id = 9, Department = new Department{Id = 4, Name = "Marketing" },  Locations = loc2 },
            new Employee {Id = 9, Department = new Department{Id = 4, Name = "Marketing" },  Locations = loc1 },
        };
    }
}

Now I want to return all locations if the department name is given. My code is working

public List<Location> GetLocations(string name)
    {
        List<Location> locations = new List<Location>();
        var collection = EmployeeRepository.AllEmployees();
        var temp = (from e in collection where e.Department.Name == name select e);
        foreach(var item in temp)
        {
            locations.AddRange(item.Locations);
        }
        
        return locations.Distinct().ToList();
        
    }

But is there a better way in one linq query/method to do that?

UPDATE:

 public class Employee
{
    public int Id { get; set; }
    public Department Department { get; set; }
    public List<Location> Locations { get; set; }

    public decimal Salary { get; set; }
}

public class Location
{
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}
public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
}
\$\endgroup\$
2
  • \$\begingroup\$ Please show the definition of Department, Location and Employee \$\endgroup\$
    – Reinderien
    Commented Mar 11, 2022 at 16:59
  • \$\begingroup\$ @Reinderien,updated. \$\endgroup\$
    – Hello
    Commented Mar 11, 2022 at 17:09

2 Answers 2

3
\$\begingroup\$

Consider using records since none of your data change.

Consider using target typing so that your list initialisation is more terse.

Don't foreach - Linq indeed has in-built support for this.

Suggested

namespace StackExchange.EmployeeExample;

public record Department(
    int Id,
    string Name
) {}

public record Location(
    string City,   
    string State,  
    string ZipCode
) {}

public record Employee(
    int Id,
    Department Department,
    List<Location> Locations,
    decimal? Salary = null
) {}                                  

public class EmployeeRepository
{
    public static List<Employee> AllEmployees
    {
        get
        {
            var loc1 = new List<Location>
            {
                new(City: "Gretna", State: "VA", ZipCode: "24587"),
                new(City: "Charlotte", State: "NC", ZipCode: "28012"),
            };
            var loc2 = new List<Location>
            {
                new(City: "Boston", State: "MA", ZipCode: "02333"),
                new(City: "Waldham", State: "MA", ZipCode: "02323"),
            };
            var loc3 = new List<Location>
            {
                new(City: "Greenville", State: "NC", ZipCode: "29011"),
            };
            var loc4 = new List<Location>
            {
                new(City: "Greenville", State: "SC", ZipCode: "26000"),
                new(City: "Charleston", State: "SC", ZipCode: "26011"),
            };

            return new List<Employee>()
            {
                new(Id: 1, Department: new Department(Id: 1, Name: "Technology"), Locations: loc1),
                new(Id: 2, Department: new Department(Id: 1, Name: "Technology"), Locations: loc2),
                new(Id: 3, Department: new Department(Id: 1, Name: "Technology"), Locations: loc3),
                new(Id: 4, Department: new Department(Id: 2, Name: "IT"), Locations: loc4),
                new(Id: 5, Department: new Department(Id: 2, Name: "IT"), Locations: loc4),
                new(Id: 6, Department: new Department(Id: 3, Name: "Sales"), Locations: loc3),
                new(Id: 7, Department: new Department(Id: 3, Name: "Sales"), Locations: loc2),
                new(Id: 9, Department: new Department(Id: 4, Name: "Marketing"), Locations: loc2),
                new(Id: 9, Department: new Department(Id: 4, Name: "Marketing"), Locations: loc1),
            };
        }
    }

    public static List<Location> GetLocations(string departmentName) =>
        AllEmployees
        .Where(e => e.Department.Name == departmentName)
        .SelectMany(e => e.Locations)
        .Distinct()
        .ToList();

    public static void Main()
    {
        var result = GetLocations("Technology");
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ One follow question. If you could answer that would be great. From the source code, which method is called for my case?(Line number) \$\endgroup\$
    – Hello
    Commented Mar 11, 2022 at 18:10
  • 2
    \$\begingroup\$ 11 probably, though any self-respecting IDE will be able to go to this definition for you \$\endgroup\$
    – Reinderien
    Commented Mar 11, 2022 at 18:14
1
\$\begingroup\$

Do you mean lambda expression?

If so you could simplify the code by using lambda expression as a parameter in the Where call, as follows:

public List<Location> GetLocations(string name)
{
    var collection = EmployeeRepository.AllEmployees();
    var locations = collection
        .Where(employee => employee.Department.Name == name)
        .SelectMany(employee => employee.Locations).Distinct().ToList();
    return locations;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.