2017-04-23 148 views
-2

外鍵我有2個相關的表,他們是集團通過與單一的LINQ

附表

SlotId 
Description 
Amount 

插槽

Id 
Name 

示例數據:

附表

slotid description amount 
-------------------------------- 
    1  Morning charge 300 
    1  Late fee   300 
    1  Earlier bonus 200 
    1  Half day   150 
    2  Morning charge 300 
    2  Late fee   300 
    2  Earlier bonus 200 
    2  Half day   150 
    3  Morning charge 300 
    3  Late fee   300 
    3  Earlier bonus 200 
    3  Half day   150 

最終結果要作爲SlotSchedules列表,像這樣:

SlotId 
    SlotName 
    List<Schedules> 

我需要:

  • 牽強時刻表
  • 後的列表中找到在不同的時隙它
  • ,然後通過每個s迭代很多和建設,我需要如下

這是我試圖與LINQ模型:

List<Schedules> schedulesAll = (from n in dbContext.Schedules         
            select n).ToList(); 

    var slotsdistinct = schedulesAll.Select(x => x.SlotId).Distinct().ToList(); 

    foreach (var slot in slotsdistinct) 
    { 
     var scheduledforslot = schedulesAll.Where(x => x.SlotId == slot).Select(x => x).ToList(); 

     foreach (Schedules _schedule in scheduledforslot) 
     { 
      //ListModel.Add(new DetailsModel { Name = _schedule.Description, Amount = (_schedule.Amount }); 
     } 
    } 

任何方式使其在單LINQ查詢?

+0

向我們展示你在C#中試了一下,然後我們會與您解決它:) – CodeNotFound

+0

@CodeNotFound請參閱編輯 –

+0

你在你的代碼只使用'Schedule'表,這夠了嗎?你不想加入表格嗎? –

回答

0

試試這個:

var slotsSchedules = dbContext.Slots.ToList().Select(slot => new { 
         SlotID = slot.Id, 
         Name = slot.Name, 
         SlotSchedules = dbContext.Schedules.ToList() 
             .Where(schedule = > schedule.SlotId == slot.Id).ToList()});