2012-07-03 145 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