我正在使用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属性
这是我为它建造的课程:
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需要&#39; pivot&#39;将所需数据放入集合内的数组中。
这是我到目前为止所做的:
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以及&#34; pivot&#34;将它们分成一个数组,适合作为JSON回发
为了使它们与儿童身份相匹配,我需要调整的值是:
sw.Employee.Emp_ID * 1000 + sw.CustomerId
这可以在LINQ语句中内联完成吗?
答案 0 :(得分:1)
事实证明这有点回答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
的参数是包含我想要“转动”by
之后的部分是将在List()
而不是数组(也许我可以使用ToArray
吗?)g
后,您可以使用g.ToList()
获取'透视'值希望有一天这会帮助其他人,甚至可能是使用visjs库的人
@Tetsuya Yamamoto小提琴指出我正确的方向