2011-02-16 30 views
1

在我的應用程序中,我必須編寫一個查詢來顯示多個表中的數據。 換句話說,我必須創建一個查詢,它將爲多個表創建一個連接,並顯示來自此查詢的所需信息。 查詢必須顯示特定(選定)工作人員的所有事件(以及有關DataGrid中事件的所有數據)。Linq查詢C#中的多個連接表#

這裏是我的LINQ的代碼:

IList dataList = (from dWorker in App.glidusContext.tbl_workers 
     where dWorker.workerTZ == strSplit 
     join d2 in App.glidusContext.tbl_workers_has_tbl_events 
     on dWorker.workerID equals d2.workerID 
     join dEvent in App.glidusContext.tbl_events 
     on d2.eventID equals dEvent.eventID 
     join dAct in App.glidusContext.tbl_activities 
     on d2.eventID equals dAct.eventID 
     select new { d2.damagedVacantionEnd, dEvent, dAct }).ToList(); 

return dataList; 

凡strSplit是具體的(選擇)的工人。數據網格的

XAML代碼:

<DataGrid.Columns> 
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_Event_type}" 
     Width="130" 
     IsReadOnly="True" 
     Binding="{Binding Path=dEvent.tbl_eventsType.eventTypeName}" /> 
    <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_Classification}" 
     Width="130" 
     Binding="{Binding Path=dEvent.tbl_eventsClassification.eventClassificationName}" /> 
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_Catagory}" 
     Width="130" 
     Binding="{Binding Path=dEvent.tbl_eventsCategories.eventCategoryName}" /> 
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_Evacuation}" 
     Width="130" 
     Binding="{Binding Path=d2.damagedEvacuationDescription}" /> 
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_EvacuationStart}" 
     Width="130" 
     Binding="{Binding Path=d2.damagedVacantionStart}" /> 
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_EvacuationEnd}" 
     Width="130" 
     Binding="{Binding Path=d2.damagedVacantionEnd}" /> 
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_ActivityName}" 
     Width="*" 
     Binding="{Binding Path=dAct.activityName}" /> 
</DataGrid.Columns> 

請幫助如何解決這個問題的查詢,現在這個變體沒有返回值。

我也不能在查詢中達到tbl_objects,因爲我需要這樣做,認爲連接表tbl_object_has_tbl_events和App.glidus不顯示這樣的表。

這裏是我的數據庫的ERD:

DB ERD

+0

如果您打開在Visual Studio數據庫的DBML文件,並在類tbl_objects_has_tbl_events顯示或不?如果有,可以打開生成的designer.cs類,複製類tbl_objects_has_tbl_events並將其粘貼到問題中?另外,如何通過手工或工具(例如sqlmetal)生成您的dbml文件? – 2011-02-16 23:36:38

+0

嗨,其實我正在使用ADO.NET實體框架並生成EDMX文件,而不是DBML。我通過Visual Studio Whizard獲得的EDMX文件,我在其中設置SQL Server實例,選擇DB並選擇此DB中的所有表。之後,它會生成ERD,我可以通過自動生成的類訪問表格。 – 2011-02-18 10:02:34

回答

1

嘗試是這樣的(我不知道到底是什麼你的導航屬性將被調用):

IList dataList = (from we in App.glidusContext.tbl_workers_has_tbl_events 
        where we.worker.workerTZ == strSplit 
        select new { we.damagedVacantionEnd, 
           we.tbl_event, 
           we.tbl_event.tbl_activity}).ToList(); 

return dataList; 

更新:

另外,看着你xaml,你正在嘗試綁定到沒有retu的屬性在你匿名類型。例如d2.damagedVacantionStart - 您尚未返回整個d2對象,因此無法綁定到其他屬性,無法訪問d2

嘗試簡化事情,方法是返回查詢中所需的確切屬性並直接綁定到這些屬性。例如

select new { VacStart = we.damagedVacantionStart, 
      VacEnd = we.damagedVacantionEnd, 
      EvTypeName = we.tbl_event.tbl_eventsType.eventTypeName, 
      ...}).ToList(); 

然後在你的XAML可以有簡單的綁定表達式:

Binding="{Binding Path=evTypeName}" />