使用 linq 填充包含数组的列表

Populating a list that contains an array using linq

我正在使用 visjs.net 显示时间表

我在编写 LINQ 来填充包含数组 属性 的列表时遇到问题

这是visjs文档visjs doco

和一些示例代码visjs source code

这是 javascript 中示例的简化版本。我需要从 MVC 控制器而不是 javascript

填充此 JSON
  var groups = new vis.DataSet();

  groups.add([
    {
      id: 1,
      content: "Lee",
      nestedGroups: [11,12]
    }
  ]);

  groups.add([
    {
      id: 11,
      content: "cook",
    },
    {
      id: 12,
      content: "shop",
    }
  ]);

剧透警报:我不知道如何填充 nestedGroups 属性

这是我为它打造的class:

    public class TimelineGroups
    {
        public int id { get; set; }
        public string content { get; set; }
        public string className { get; set; }
        public string orderBy { get; set; }
        public string[] nestedGroups { get; set; }
    }

这是填充它的控制器方法,不包括 nestedGroups 属性,并且工作正常:

    public JsonResult EmployeeGroups()
    {
        List<TimelineGroups> e = (
            from emp in db.Employees
            where emp.Emp_Status == 1 && emp.EmployeeRole.IsChargeable == "Y"
            orderby emp.EmpRole_ID, emp.Emp_Last_Name
            select new TimelineGroups()
            {
                id = emp.Emp_ID,
                content = emp.Emp_Name,
                orderBy = emp.EmpRole_ID.ToString(),
                className = "bte-" + emp.EmployeeRole.EmpRoleName
            }
            ).ToList();

        return Json(e, JsonRequestBehavior.AllowGet);
    }

一切正常,但现在我想添加嵌套组。

但我不知道需要 LINQ 'pivot' 将所需数据放入集合中的数组中。

这是我目前拥有的:

public JsonResult EmployeeClientGroups()
{
    // First load the client list which are children of employees
    List<TimelineGroups> cg = (
        from sw in db.ScheduleWorks
        where sw.EndDate > DateTime.Now
        select new TimelineGroups()
        {
            id = sw.Employee.Emp_ID * 1000 + sw.CustomerId,
            content = sw.Customer.Customer_Name
        }).Take(1000).Distinct().ToList();

    // now load the employee list
    // This includes client children
    List<TimelineGroups> eg = (
        from sw in db.ScheduleWorks
        where sw.EndDate > DateTime.Now
        select new TimelineGroups()
        {
            id = sw.Employee.Emp_ID,
            // How do I populate this with sw.Employee.Emp_ID * 1000 + sw.CustomerId
            nestedGroups = // What magic do I put here?
            content = sw.Employee.Emp_Name,
        }).Take(1000).Distinct().ToList();

    // Add the employee groups to the client groups
    cg.AddRange(eg);

    return Json(cg, JsonRequestBehavior.AllowGet);

}

我需要获取给定员工的所有客户 ID,并将它们 "pivot" 放入一个数组中,适合 post 返回为 JSON

为了使它们独一无二并匹配 children ID,我需要转换的值是:

sw.Employee.Emp_ID * 1000 + sw.CustomerId

这可以在 LINQ 语句中内联完成吗?

事实证明这是一种回答here但我会记录我的解决方案

这是我最终使用的代码:

    public class TimelineGroups
    {
        public string id { get; set; }
        public string content { get; set; }
        public string className { get; set; }
        public string orderBy { get; set; }
        public List<string> nestedGroups { get; set; }
    }


        List<TimelineGroups> eg = (
            from sw in db.ScheduleWorks
            where sw.EndDate > DateTime.Now
            where sw.Employee.Emp_Status == 1

            group (sw.EmployeeId.ToString() + "." + sw.CustomerId.ToString())
            by new {
                id = sw.EmployeeId.ToString(),
                EmpName = sw.Employee.Emp_Name,
                EmpOrder = sw.Employee.EmpRole_ID.ToString()
            }

            into g

            select new TimelineGroups()
            {
                id = g.Key.id,
                // find the list of employee+client keys 
                nestedGroups = g.ToList(),
                content = g.Key.EmpName,
                orderBy = g.Key.EmpOrder
            })
            .Take(1000)
            .ToList();
  • 备注无Distinct
  • group 的参数是包含我想要 "pivot"
  • 的值的字段
  • 那么 by 之后的部分就是实际 'key' 将在
  • 上分组
  • 我需要使用 List() 而不是数组(也许我可以使用 ToArray?)
  • 一旦将其分组为中间变量 g,您可以使用 g.ToList() 获取 'pivoted' 值

希望有一天这会对其他人有所帮助,甚至可能对使用 visjs 库的人有所帮助

@Tetsuya Yamamoto fiddle 给我指明了正确的方向