1

我是 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>

但是我们想要这样的数据表输出 预期产出

提前致谢!

4

1 回答 1

0

LINQ 可以通过允许您from在查询中嵌套子句(稍微简化输出)来帮助您:

from DataListItem dli in PollDataList.Items
let z = ... // as in existing code
let y = ...
let v = ...
from checkbox in v.Items
where checkbox.Selected
select new [] { z.Value, checkbox.Value, "" }

在这里,您将获得每个投票项目(您的)中每个选定复选框的输出项目Answered2。但是您还需要每个轮询项的值的输出行Answered1,而这里 LINQ 的帮助较小。有两种方法可以做到这一点,使用Enumerable.Concat的快速而丑陋的方法:

...
let v = ...
from answer in new [] { y.SelectedValue }.Concat(v.Items.Where(r => r.Selected)) // http://msdn.microsoft.com/en-us/library/bb302894.aspx
select new [] { z.Value, answer, "" }

或者更清晰、更通用但更长的方法,yield用于创建一个迭代器方法,为您提供所有答案的项目:

from DataListItem item in PollDataList.Items
let z = ...
from answer in PollItemAnswers(item)
...
// outside the method containing the above
IEnumerable<object> PollItemAnswers(PollListItem item)
{
    var y = ...; // As in let statements
    var v = ...;
    yield return y.Value; // return the first answer
    foreach (var checkBox in v.Items)
        if (checkBox.Selected)
            yield return checkBox.Value; // return the other answers
}

最后,如果您的最终代码没有将项目分开(例如,它是从网络表单发布的),您可以使用string.Split()

 yield return y.Value;
 foreach (var answer in value.Split(',')) // "a, b, c" -> "a", " b", " c"
     yield return answer.Trim(); // " b" -> "b"

最后一点,请注意您的答案是否包含,!一般来说,使用 JSON 之类的东西来安全地移动值集合(比如从文件或另一台机器)将为您节省很多麻烦。

于 2013-06-07T08:21:43.730 回答