I am new to linq. I am using linq to get vlaues from datalist control and creating a datatable with that values. The datatable contains three columns. In that, sometimes the 'Answer' column is having a data which is comma separated.
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();
And here is my design
<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>
But we want the datatable output like this
Thanks in advance!
new object[]
you need to see if p.Answered2 is not empty/null. If not, meaning there is a comma separated value to view in separate rows, perform the first insert with a new object with p.Answered1, then again with another row and p.Answered2. – C. Lang 14 hours ago