2

我正在做以下工作

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>

编辑以根据要求显示标记/绑定

4

1 回答 1

2

您的CompletedBy字段实际上被键入为具有单个字符串字段的类的集合。因为您使用的是 aselect 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()
于 2012-07-03T14:40:19.380 回答