如何计算链式linq查询中的属性

时间:2013-12-04 22:30:51

标签: c# linq

是否可以根据DivisionDepartment计算“即时”JobTitle 最终应用了一些转换,例如连接 在下面的链式linq查询中?

  public static List<Developer> GetDevelopersData(List<Employee> employees)
    {
        List<Developer> developers = 
        employees.Where(x => x.Department == "Dev")
                 .Select(x => new Developer
                     {
                         Name = x.Name,
                         Department = x.Department,
                         JobTitle = x.Function,
                         Division = "Department" + "/" + "Function" // based on previous properties
                     }).ToList();

        return developers;
    }

3 个答案:

答案 0 :(得分:1)

我认为Function和Departement是字符串类型?

    List<Developer> developers =
    employees.Where(x => x.Department == "Dev")
             .Select(x => new Developer
             {
                 Name = x.Name,
                 Department = x.Department,
                 JobTitle = x.Function,
                 Division = String.Concat(x.Function, "/", x.Department)
             }).ToList();

    return developers;

答案 1 :(得分:0)

除非我遗漏了什么,否则你可以这样做:

Division = string.Format("{0}/{1}", x.Department, x.Function)

假设您只想将两个属性连接成一个字符串。

答案 2 :(得分:0)

要动态计算,您可以使用

 (from x in employees
 let MyFunction=String.Concat(x.Function, "/", x.Department)//you can use whatever you want here
 select new Developer()
 {
       Name = x.Name,
       Department = x.Department,
       JobTitle = x.Function,
       Division = x.MyFunction
  }).ToList();