我是 linq 的新手。我正在使用 linq 从 datalist 控件中获取 vlaues 并使用该值创建数据表。数据表包含三列。在那里,有时“答案”列包含逗号分隔的数据。
DataTable SaveAnswer = clsSource.SetData();
var df = from DataListItem dli in PollDataList.Items
let z = ((HiddenField)dli.FindControl("IDReqHiddenField"))
let y = ((RadioButtonList)dli.FindControl("rdblstPollOptions"))
let v = ((CheckBoxList)dli.FindControl("CheckBoxListMultiplePollOptions"))
select new
{
PollId=z.Value,
Answered1 = y.SelectedValue,
Answered2=v.Items.Cast<ListItem>().Where(r=>r.Selected)
};
var result = from p in df
select SavePollAnswer.LoadDataRow(
new object[] {
p.PollId,p.Answered1+string.Join( ", ", p.Answered2 ),""
},
false);
SavePollAnswer = result.CopyToDataTable();
这是我的设计
<asp:DataList ID="PollDataList" runat="server" CssClass="poll-preview">
<ItemTemplate>
<asp:HiddenField ID="PollIDReqHiddenField" Value='<%# Eval("PollID") %>' runat="server" Visible="false" />
<asp:Label ID="lblReqQuestionNumber" runat="server" Text='<%# Eval("NoofPollQuestion") %>' Font-Bold="true"></asp:Label><br />
<asp:Label ID="lblRequiredPollQusetion" runat="server" Text='<%# Eval("PollQuestions") %>' Font-Bold="false"></asp:Label>
<asp:Label ID="lblReqNotification" runat="server" ForeColor="Red" CssClass='<%# Eval("PollReq") %>' Text="*" Font-Bold="true"></asp:Label>
<asp:RadioButtonList ID="rdblstPollOptions" DataSource='<%# Eval("PollOptionsReq") %>' runat="server"></asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxListMultiplePollOptions" DataSource='<%# Eval("PollOptionsMul") %>' runat="server" RepeatDirection="Vertical" RepeatColumns="1"></asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
但是我们想要这样的数据表输出
提前致谢!