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
                      }                                 
    };

ご覧のとおり、ここでは 2 つのワーカー オブジェクトを使用しています。1 つは年次/中間雇用評価を受けているワーカーで、もう 1 つはライン マネージャーです。

この選択は要約リスト ビュー コントロールをバインドするために使用されますが、割り当てようとしている CompletedBy 列には常に null として最新のエントリがあり、評価が完了すると、履歴データにライン マネージャーの名前が入力されます。 . この質問について簡単な検索を行ったところ、回答付きの投稿がたくさんありますが、開発者としての経験があまりなく、簡単な回答を探しています。私のもう1つの懸念は、リストに常にnull値があることがわかっている列を割り当てようとしていることです。

コンパイラはこれを構文的に正しいと認識しますが、バインド時に私のラベル テキスト プロパティは次のようになります...

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は、実際には単一の文字列フィールドを持つクラスのコレクションとして入力されます。を使用しているため、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()
于 2012-07-03T14:40:19.380 に答える