如何使用Linq C#拆分数据表中的字符串并添加到新行中?

时间:2013-06-07 07:23:11

标签: c# asp.net linq

我是linq的新手。我正在使用linq从datalist控件获取vlaues并使用该值创建数据表。数据表包含三列。在这种情况下,有时“答案”列的数据以逗号分隔。 You can see the image of datatable with comma separated column

            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>

但是我们想要像这样的数据表输出 Expected Output

提前致谢!

1 个答案:

答案 0 :(得分: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创建iterator method,为您提供所有答案的项目:

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的东西来安全地移动值集合(例如从文件或其他机器),将为您带来很多悲伤。