Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Other warnings notwithstanding, providing a LINQ solution is worth considering even if it ultimately isn't the right solution for this problem.</p> <p>I'll offer a LINQ solution, with the caveats that a proper production-worthy solution should do error checking and argument validation including checks that the file is properly formed that are interesting but beyond the scope of what is requested here and often a bit of a challenge to do completely functionally.</p> <pre><code>var myList = File.ReadLines(myFile.ToString(), Encoding.GetEncoding(1250)) .Select(line =&gt; { var split = line.Split(','); return new specialclass { Comp = split[0], Place = Convert.ToInt32(split[1]), Name = split[2], Limit = Convert.ToInt32(split[3]), Limit2 = Convert.ToInt32(split[4]) }; }) .ToList(); var itemsAndPrevious = new specialclass [] { null } .Concat(myList) .Zip(myList, (prev,item) =&gt; new { prev, item }); var itemsWithoutError = itemsAndPrevious .Where(i =&gt; i.item.Comp != "Error" // omit error line &amp;&amp; (i.prev == null || i.prev.Comp != "Error")) // omit line following error lines .Select(i =&gt; i.item) .OrderBy(i =&gt; i.Comp); var itemsWithError = itemsAndPrevious.Where(i =&gt; i.prev != null &amp;&amp; i.prev.Comp == "Error") .OrderBy(i =&gt; i.item.Comp) .SelectMany(i =&gt; new [] { i.prev, i.item }); var desiredResult = itemsWithoutError.Concat(itemsWithError); </code></pre> <p>If you plan on running this only on data with anywhere from a few hundred or even a few thousand entries, this may perform well enough that you won't need to find more efficient solutions. What is important is that you don't assume that any implementation will be fast enough - time them, just to be sure. You should also see how doubling the input size affects the run time of the code - then you can form predictions on how well this will perform on larger and larger input sizes.</p> <p>I offer no warranty that the above code is either tested or bug-free. <code>Zip</code> was added in 4.0 so it won't be available if you are using a framework prior to that. Otherwise, implementing your own <code>Zip</code> is a worthwhile exercise. Happy coding.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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