我正在研究以下内容
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列上将始终具有最新条目为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>
编辑以根据要求显示标记/绑定
答案 0 :(得分: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()