2017-06-12 123 views
1

我使用visjs.net顯示日程表填充包含使用LINQ

我無法寫LINQ填充包含一個屬性,它是一個數組

下面列表中的數組列表是visjs文檔visjs doco

和一些示例代碼visjs source code

這裏以JavaScript樣品的刪節版。我需要從一個MVC控制器,而不是JavaScript的

var groups = new vis.DataSet(); 

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

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

擾流警報填充這個JSON:我不知道該如何填寫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需要將所需數據「旋轉」到集合中的數組中。

這是我到目前爲止有:

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對一個給定的僱員和「轉動」他們到一個數組,適用於回發的JSON

爲了使他們獨特而符合兒童的id,我需要在轉動的值是:

sw.Employee.Emp_ID * 1000 + sw.CustomerId

這能在LINQ聲明在線做了什麼?

+0

AFAIK你可以在LINQ'select'中執行加法,但在這種情況下,你試圖將'int'值插入到'string []'數組中當然是無效的演員。檢查這個簡化的小提琴,讓我知道是否將'new string []''聲明爲'nestedGroups'是你想要做的:https://dotnetfiddle.net/40lDp9。 –

+0

感謝您花時間設置小提琴。我今晚會嘗試這個解決方案。我希望這真的很簡單! –

+0

對於您準備好的示例數據,我應該看到列表中的一個成員,其中Id = 1234,nestedGroups =(數組爲1,2,3,4,5,6)。我試過後會回覆給你 –

回答

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小提琴把我指向正確的方向