<p>Being relatively new to the .net game, I was wondering, has anyone had any experience of the pros / cons between the use of LINQ and what could be considered more traditional methods working with lists / collections?  </p>

<p>For a specific example of a project I'm working on : a list of unique id / name pairs are being retrieved from a remote web-service.</p>

<ul>
<li>this list will change infrequently (once per day), </li>
<li>will be read-only from the point of view of the application where it is being used</li>
<li>will be stored at the application level for all requests to access</li>
</ul>

<p>Given those points, I plan to store the returned values at the application level in a singleton class.</p>

<p>My initial approach was to iterate through the list returned from the remote service and store it in a NameValueCollection in a singleton class, with methods to retrieve from the collection based on an id:</p>

<pre><code>sugarsoap soapService = new sugarsoap();
branch_summary[] branchList = soapService.getBranches();

foreach (branch_summary aBranch in branchList)
{
    branchNameList.Add(aBranch.id, aBranch.name);
}
</code></pre>

<p>The alternative using LINQ is to simply add a method that works on the list directly once it has been retrieved:</p>

<pre><code>public string branchName (string branchId) 
{
    //branchList populated in the constructor
    branch_summary bs = from b in branchList where b.id == branchId select b;
    return branch_summary.name;
}
</code></pre>

<p>Is either better than the other - is there a third way?  I'm open to all answers, for both approaches and both in terms of solutions that offer elegance, and those which benefit performance.</p>

[{'answer_id': 48476, 'author': 'Rob Stevenson-Leggett', 'author_id': 4950, 'author_profile': 'https://Stackoverflow.com/users/4950', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not sure a singleton class is absolutely necessary, do you absolutely need global access at all times? Is the list large? </p>\n\n<p>I assume you will have a refresh method on the singleton class for when the properties need to change and also that you have some way of notifying the singleton to update when the list changes.</p>\n\n<p>Both solutions are viable. I think LINQ will populate the collection faster in the constructor (but not noticeably faster). Traditional collection based approaches are fine. Personally, I would choose the LINQ version if only because it is new tech and I like to use it. Assuming your deployment environment has .NET 3.5...</p>\n\n<p>Do you have a method on your webservice for getting branches by Id? That would be the third option if the branch info is needed infrequently.</p>\n"}, {'answer_id': 48545, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 3, 'selected': True, 'text': "<p>i dont think the linq you wrote would compile, it'd have to be</p>\n\n<pre><code>public string branchName (string branchId) \n{\n    //branchList populated in the constructor\n    branch_summary bs = (from b in branchList where b.id == branchId select b).FirstOrDefault();\n    return branch_summary == null ? null : branch_summary.name;\n}\n</code></pre>\n\n<p>note the .FirstsOrDefault()</p>\n\n<p>I'd rather use LINQ for the reason that it can be used in other places, for writing more complex filters on your data.  I also think it's easier to read than NameValueCollection alternative.</p>\n\n<p>that's my $0.02</p>\n"}, {'answer_id': 1130367, 'author': 'Sam Harwell', 'author_id': 138304, 'author_profile': 'https://Stackoverflow.com/users/138304', 'pm_score': 1, 'selected': False, 'text': '<p>In general, your simple one-line for/foreach loop will be faster than using Linq. Also, Linq doesn\'t [always] offer significant readability improvements in this case. Here is the general rule I code by:</p>\n\n<p>If the algorithm is simple enough to write and maintain without Linq, and you don\'t need delayed evaluation, <em>and</em> Linq doesn\'t offer sufficient maintainability improvements, then don\'t use it. However, there are times where Linq <em>immensely</em> improves the readability and correctness of your code, as shown in two examples I posted <a href="http://blog.280z28.org/archives/2008/10/20/" rel="nofollow noreferrer">here</a> and <a href="http://blog.280z28.org/archives/2008/10/24/" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 12652310, 'author': 'SimpleVar', 'author_id': 1282539, 'author_profile': 'https://Stackoverflow.com/users/1282539', 'pm_score': 0, 'selected': False, 'text': '<p>Shortened and workified:</p>\n\n<pre><code>public string BranchName(string branchId) \n{\n    var bs = branchList.FirstOrDefault(b =&gt; b.Id == branchId);\n\n    return bs == null ? null : bs.Name;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/48432', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4396/']