Well, I would expect this line to throw an exception:
var documentRow = _dsACL.Documents.First(o => o.ID == id)
First() will throw an exception if it cannot find suitable elements. Given that you immediately check for a null value, it looks like you want FirstOrDefault() , which returns the default value for an element type (which is null for reference types) if no matching elements are found:
var documentRow = _dsACL.Documents.FirstOrDefault(o => o.ID == id)
Other options to consider in some situations: Single() (when you think that there is only one matching element) and SingleOrDefault() (when you think that there is only one or zero matching element). I suspect that FirstOrDefault is the best option in this particular case, but he still knows about others.
On the other hand, it seems that in fact it may be more profitable for you to join you in the first place. If you didn’t care that it would make all matches (and not just the first ones), you can use:
var query = from target in _lstAcl.Documents join source in _dsAcl.Document where source.ID.ToString() equals target.ID select new { source, target }; foreach (var pair in query) { target.Read = source.Read; target.ReadRule = source.ReadRule;
This is a simpler and more effective IMO.
Even if you decide to keep the loop, I have a few suggestions:
- Get rid of the external
if . You do not need this, as if Count was zero, the body of the loop cycle will never execute Use exclusive upper bounds for loops - they are more idiomatic in C #:
for (i = 0; i < _lstAcl.Documents.Count; i++)
Eliminate common subexpressions:
var target = _lstAcl.Documents[i]; // Now use target for the rest of the loop body
If possible, use foreach instead of for to start with:
foreach (var target in _lstAcl.Documents)
Jon Skeet Oct 22 '10 at 6:14 2010-10-22 06:14
source share