Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>LINQ can get you partway by allowing you to nest <code>from</code> clauses in queries (simplifying the output a bit):</p> <pre><code>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, "" } </code></pre> <p>Here you will get an output item for each selected checkbox in each poll item (your <code>Answered2</code>). But you also want an output row for the <code>Answered1</code> value for each poll item, and here LINQ is less helpful. There are two ways you can do this, the quick and ugly way using <a href="http://msdn.microsoft.com/en-us/library/bb302894.aspx" rel="nofollow">Enumerable.Concat</a>:</p> <pre><code>... let v = ... from answer in new [] { y.SelectedValue }.Concat(v.Items.Where(r =&gt; r.Selected)) // http://msdn.microsoft.com/en-us/library/bb302894.aspx select new [] { z.Value, answer, "" } </code></pre> <p>Or the clearer and more general, but longer way, using <code>yield</code> to create an <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx" rel="nofollow">iterator method</a> that gives you an item for all the answers:</p> <pre><code>from DataListItem item in PollDataList.Items let z = ... from answer in PollItemAnswers(item) ... // outside the method containing the above IEnumerable&lt;object&gt; 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 } </code></pre> <p>And finally, if your final code don't have the items already separated (say, it was posted from a web form), you can use <code>string.Split()</code>:</p> <pre><code> yield return y.Value; foreach (var answer in value.Split(',')) // "a, b, c" -&gt; "a", " b", " c" yield return answer.Trim(); // " b" -&gt; "b" </code></pre> <p>As a final note, watch out for if your answers could contain <code>,</code>! In general, using something like JSON to safely move value collections around (say from a file or another machine) will save you a lot of grief.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload