I agree with Ed Swangren. It looks concise and readable.
Actually the answer to your question depends on 3 things:
- What do you want to achieve - better readability? better performance? and etc.
- Account Type
- How will the resulting collection be used.
If you need better performance, and if the โaccountsโ are a list and the resulting collection is iterated or passed to another method to iterate soon enough after these lines of code, I would do something like this:
List<Account> filteredAccounts = new List<Account>(); accounts.ForEach(a => { if (a.AccountProjectID == accountProjectId) filteredAccounts.Add(a); });
Of course, this is less readable than your LINQ statement, but I would use these 2 lines, not the accounts. Choose .......
And of course, it is much better optimized for performance, and this is always important.
Alexander
source share