Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I'm new to LINQ, so any advice is appreciated!</p> </blockquote> <p>Linq is mostly designed around querying, aggregating, and filtering data, not around directly modifying existing data. You can use it to make a modified copy of your data, and overwrite the original data with this copy.</p> <p>The problem you're having isn't exactly because of Linq, though.</p> <p>Lets go over your code:</p> <pre><code>string[] asmInfo = File.ReadAllLines(file); </code></pre> <p>You're copying all the lines of the file into an in-memory string array.</p> <pre><code>var line = asmInfo.Single(x =&gt; x.Trim().StartsWith("[assembly: AssemblyVersion")); </code></pre> <p>You're querying all the lines, and copying out the line (more-or-less by value) into the variable <code>line</code>.</p> <pre><code>line = "[assembly: AssemblyVersion\"" + version + "\")]"; </code></pre> <p>You're writing over your copied line with different content. This does not modify the original array.</p> <pre><code>File.WriteAllLines(file, asmInfo); </code></pre> <p>You're writing back the original array to the file. You didn't change the array, so you won't see any change in the file.</p> <p>A few ways to solve this:</p> <ul> <li>Copy out the array, as you're currently doing, find the index of the line you want to change, and modify the array (by index). This option doesn't use Linq at all.</li> <li>Use linq to make a transformed copy of the data, and write the whole transformed copy back. This option uses linq, but doesn't leverage its power.</li> <li>Stop using <code>File.ReadAllLines</code>/<code>File.WriteAllLines</code>, and read/write one line at a time. This option actually leverages the power of Linq, but is the most complicated.</li> </ul> <p><strong>Modify the array</strong></p> <p>This method doesn't use Linq at all:</p> <pre><code>string[] asmInfo = File.ReadAllLines(file); int lineIndex = Array.FindIndex(asmInfo, x =&gt; x.Trim().StartsWith("[assembly: AssemblyVersion")); asmInfo[lineIndex] = "[assembly: AssemblyVersion\"" + version + "\")]"; File.WriteAllLines(file, asmInfo); </code></pre> <p>This is a fairly standard way to accomplish the task, assuming your file sizes are small.</p> <p><strong>Query and make a transformed copy of the data, and write out the copy</strong></p> <p>This method is more Linq-like:</p> <pre><code>string[] asmInfo = File.ReadAllLines(file); var transformedLines = asmInfo.Select( x =&gt; x.Trim().StartsWith("[assembly: AssemblyVersion") ? "[assembly: AssemblyVersion\"" + version + "\")]" : x ); File.WriteAllLines(file, asmInfo.ToArray()); </code></pre> <p>This is more Linq-like than the previous example. But it is actually less efficient because <code>File.WriteAllLines</code> cannot take an <code>IEnumerable&lt;string&gt;</code>. Because of this, you are forced to call <code>ToArray</code>. When you call <code>ToArray</code> (or <code>ToList</code>), the entire query is run and copied into a new array, so you have two copies of the file sitting around.</p> <p>If <code>File.WriteAllLines</code> took an <code>IEnumerable&lt;string&gt;</code>, then you wouldn't have to make an extra copy, and each line would be written while the query was still running.</p> <p><strong>Read and write one line at a time</strong></p> <p>This option is the most efficient, and actually shows some of the benefits of Linq - more specifically, of <code>IEnumerable</code>, which is where most of Linq's power comes from.</p> <pre><code>public static class FileStreamExtensions { public static IEnumerable&lt;string&gt; GetLines(this FileStream stream) { using (var reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) yield return line; } } public static void WriteLines(this FileStream stream, IEnumerable&lt;string&gt; lines) { using (var writer = new StreamWriter(stream)) { foreach (string line in lines) { writer.WriteLine(line); } } } } private void UpdateVersion(string file, string version) { using (FileStream inputFile = File.OpenRead(file)) { string tempFilePath = Path.GetTempFileName(); var transformedLines = inputFile.GetLines().Select( x =&gt; x.Trim().StartsWith("[assembly: AssemblyVersion") ? "[assembly: AssemblyVersion\"" + version + "\")]" : x ); using (FileStream outputFile = File.OpenWrite(tempFilePath)) { outputFile.WriteLines(transformedLines); } string backupFilename = Path.Combine(Path.GetDirectoryName(file), Path.GetRandomFileName()); File.Replace(tempFilePath, file, backupFilename); } } </code></pre> <p>This requires a lot more code because:</p> <ul> <li>The intermediate copy of your data is now in a temp file, instead of an in-memory array.</li> <li>The stream classes don't themselves provide line-by-line enumerators, so we built them ourselves.</li> </ul> <p>You'll see some interesting things if you run it in the debugger, and set break points on the <code>yield return line</code> and <code>writer.WriteLine</code> statements. The read code and the write code are (more or less) running at the same time. First a line is read, then it is written.</p> <p>This is because of <code>IEnumerable</code> and <code>yield return</code>, and this is one of the main reasons Linq is more than just syntactic sugar.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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