2012-07-03 141 views
2

我在下面簡單的LINQ to SQL嵌套查詢

var result = 
    from ap in db.Appraisals 
    join at in db.AppraisalTypes 
     on ap.AppraisalType_ID equals at.AppraisalType_ID 
    join w in db.Workers 
     on ap.Worker_ID equals w.Worker_ID 
    where ap.Worker_ID == WorkerID 
      && ap.DateDue != null 
      && ap.DateCompleted == null 
    select new 
    { 
     ap.Worker_ID, 
     ap.Appraisal_ID, 
     at.Description, 
     ap.DateDue, 
     ap.DateCompleted, 
     CompletedBy = from ap2 in db.Appraisals 
         join w2 in db.Workers 
          on ap2.CompletedBy equals w2.Worker_ID 
         select new 
         { 
          name = w2.Surname + ", " + w2.FirstName 
         }         
    }; 

工作正如你可能會看到我使用兩個工作目標,其中之一是是有其年度/中期就業評價和職工其他對象是他們的直線經理。

此選擇用於綁定摘要列表視圖控件,但在我試圖分配的CompletedBy列上始終將最新的條目設置爲空,那麼歷史數據將填充行管理器名稱評估工作已經完成。我已經在這個問題上做了一個快速搜索,並且有許多帖子有答案,但我作爲一名開發人員並不是非常有經驗,並且正在尋找一個簡單的答案。我的另一個擔心是,我想分配一個列,我知道列表中總是有一個空值。

雖然編譯器可以識別這爲語法正確的,在我的綁定標籤文本屬性有以下...

System.Collections.Generic.List`1[<>f__AnonymousType9`1[System.String]] 

我只是在尋找任何可能的警告我在這裏建設並填寫我對LINQ的知識非常有限。

<asp:ListView ID="lvwProbations" runat="server" 
    OnSelectedIndexChanged="lvwProbations_SelectedIndexChanged" 
    OnPagePropertiesChanged="lvwProbations_PagePropertiesChanged" 
    OnPagePropertiesChanging="lvwProbations_PagePropertiesChanging" 
    DataKeyNames="Worker_ID,Appraisal_ID"> 

    <ItemTemplate> 
     <tr class="tableItemStyle" 
      onmouseover="this.style.backgroundColor='Silver'"  
      onmouseout="this.style.backgroundColor='#EAFFFF'"> 

      <td> 
       <asp:Label ID="lblWorkerID" runat="server" 
        Text='<%# Bind("Worker_ID") %>' Visible="false" /> 
      </td> 
      <td> 
       <asp:Label ID="lblProbation" runat="server" Width="200" 
        Text='<%# Bind("Description") %>' /> 
      </td> 
      <td align="center"> 
       <asp:Label ID="lblDateDue" runat="server" Width="100" 
        Text='<%# Bind("DateDue", "{0:d}") %>' /> 
      </td> 
      <td align="center"> 
       <asp:Label ID="lblDateCompleted" runat="server" Width="100" 
        Text='<%# Bind("DateCompleted") %>' /> 
      </td> 
      <td align="center"> 
       <asp:Label ID="lblCompletedBy" runat="server" Width="100" 
        Text='<%# Bind("CompletedBy") %>' /> 
      </td> 
      <td> 
       <asp:ImageButton ID="btnSelect" runat="server" 
        SkinID="selecttimesheet" CommandName="Select" /> 
      </td> 
     </tr> 
    </ItemTemplate> 

    <AlternatingItemTemplate> 
     <tr class="tableAlternatingItemStyle" 
      onmouseover="this.style.backgroundColor='Silver'"   
      onmouseout="this.style.backgroundColor='#CCFFFF'"> 
      <td> 
       <asp:Label ID="lblWorkerID" runat="server" 
         Text='<%# Bind("Worker_ID") %>' Visible="false" /> 
      </td> 
      <td> 
       <asp:Label ID="lblProbation" runat="server" Width="200" 
         Text='<%# Bind("Description") %>' /> 
      </td> 
      <td align="center"> 
       <asp:Label ID="lblDateDue" runat="server" Width="100" 
         Text='<%# Bind("DateDue", "{0:d}") %>' /> 
      </td> 
      <td align="center"> 
       <asp:Label ID="lblDateCompleted" runat="server" Width="100" 
         Text='<%# Bind("DateCompleted") %>' /> 
      </td> 
      <td align="center"> 
       <asp:Label ID="lblCompletedBy" runat="server" Width="100" 
         Text='<%# Bind("CompletedBy") %>' /> 
      </td> 
      <td> 
       <asp:ImageButton ID="btnSelect" runat="server" 
         SkinID="selecttimesheet" CommandName="select" /> 
      </td> 
      </tr> 
     </AlternatingItemTemplate> 

     <EmptyDataTemplate> 
      <table id="Table1" runat="server" style=""> 
      <tr id="Tr2" runat="server" class="tableHoursHeaderStyle"> 
       <th id="Th1" runat="server" style="width:0px;"></th> 
       <th id="Th2" runat="server">Appraisal</th> 
       <th id="Th3" runat="server">Date Due</th> 
       <th id="Th13" runat="server">Date Completed</th> 
       <th id="Th4" runat="server">Completed By</th> 
       <th id="Th6" runat="server" style="width:0px;"></th> 
      </tr> 
      <tr> 
       <td colspan="4"> 
         <p>No Probations available</p> 
       </td> 
      </tr> 
     </table> 
    </EmptyDataTemplate> 

    <LayoutTemplate> 
     <table id="Table2" runat="server"> 
      <tr id="Tr1" runat="server"> 
       <td id="Td1" runat="server"> 
        <table ID="itemPlaceholderContainer" runat="server" 
          border="0" class="TimeSheet_Table" style=""> 
        <tr id="Tr2" runat="server" 
         class="tableHoursHeaderStyle"> 
          <th id="Th1" runat="server" style="width:0px;"></th> 
          <th id="Th2" runat="server">Appraisal</th> 
          <th id="Th3" runat="server">Date Due</th> 
          <th id="Th13" runat="server">Date Completed</th> 
          <th id="Th5" runat="server">Completed By</th> 
          <th id="Th7" runat="server" style="width:0px;"></th> 
        </tr> 
        <tr ID="itemPlaceholder" runat="server"></tr> 
        </table> 
       </td> 
       </tr> 
      </table> 
     </LayoutTemplate> 
</asp:ListView> 

編輯,以顯示要求

+0

不知道問題是什麼,你能更清楚了嗎? – Maghis

+0

道歉,基本上嵌套選擇綁定System.Collections代碼片段如上。我需要獲得直線經理的名字,因爲每次對特定員工的最新評估都會對CompletedBy字段返回空值。代碼片段的含義是什麼?我對於返回數據記錄/列的嘗試有何不瞭解? – dotnetnewb

+0

讓我看看你的標籤綁定。 – WinnerIT

回答

2

CompletedBy場標記/綁定實際上是類型爲具有單一字符串字段一個類的集合。由於您使用的是select new { name = ... }你實際上是在創建一個類,它看起來像這樣是不是你想要的所有東西:

class Anonymous9 
{ 
    public name { get; set; } 
} 

而且,除此之外,你正在創建一個IEnumerable<Anonymous9>這是這些對象的集合。

相反,你只需要一個字符串。爲了得到它只是一個字符串,擺脫new {},並與FirstOrDefault()包起來:

CompletedBy = (from ap2 in db.Appraisals 
        join w2 in db.Workers 
         on ap2.CompletedBy equals w2.Worker_ID 
        select w2.Surname + ", " + w2.FirstName 
       ).FirstOrDefault() 
+0

+1 w/Answer接受:)非常感謝。我需要了解很多關於我意識到的一些基本概念。 – dotnetnewb