Answered by:
Convert foreach to LINQ

Question
-
Hi,
I have code like this
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); var list = new List<EmergencyScenario>(); foreach (var item in emergencyScenarios) { if (item.EmergencyEvents != null) { foreach (var events in item.EmergencyEvents) { if (events.IsEmergencyDrill == true) { list.Add(item); } } } }
It works but I dont think is clean, because too many "if statements inside foreach".
Is it possible to convert this code in LINQ?
Many Thanks :)
- Edited by wapt49 Friday, February 27, 2015 10:58 AM
Answers
-
Try this
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); List<EmergencyScenario> list = emergencyScenarios.AsEnumerable() .Where(x => x.EmergencyEvents != null) .Where(y => y.EmergencyEvents.IsEmergencyDrill == true) .ToList();
jdweng
- Proposed as answer by Magnus (MM8)MVP Friday, February 27, 2015 11:19 AM
- Marked as answer by wapt49 Monday, March 2, 2015 1:31 PM
-
Apparently the "item.EmergencyEvents" is enumerable so I think the request is :
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); List<EmergencyScenario> list = emergencyScenarios.AsEnumerable() .Where(x => x.EmergencyEvents != null) .Where(y => y.EmergencyEvents.Any(z => z.IsEmergencyDrill == true)) .ToList();
And we can reduce to
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); List<EmergencyScenario> list = emergencyScenarios.AsEnumerable() .Where(x => x.EmergencyEvents != null && x.EmergencyEvents.Any(z => z.IsEmergencyDrill == true)) .ToList();
Regards,
- Edited by Yan Grenier - MTFC Friday, February 27, 2015 11:39 AM
- Marked as answer by wapt49 Monday, March 2, 2015 1:31 PM
All replies
-
Try this
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); List<EmergencyScenario> list = emergencyScenarios.AsEnumerable() .Where(x => x.EmergencyEvents != null) .Where(y => y.EmergencyEvents.IsEmergencyDrill == true) .ToList();
jdweng
- Proposed as answer by Magnus (MM8)MVP Friday, February 27, 2015 11:19 AM
- Marked as answer by wapt49 Monday, March 2, 2015 1:31 PM
-
Apparently the "item.EmergencyEvents" is enumerable so I think the request is :
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); List<EmergencyScenario> list = emergencyScenarios.AsEnumerable() .Where(x => x.EmergencyEvents != null) .Where(y => y.EmergencyEvents.Any(z => z.IsEmergencyDrill == true)) .ToList();
And we can reduce to
var emergencyScenarios = this.db.EmergencyScenarioRepository.GetQueryableAsNoTracking(filter); List<EmergencyScenario> list = emergencyScenarios.AsEnumerable() .Where(x => x.EmergencyEvents != null && x.EmergencyEvents.Any(z => z.IsEmergencyDrill == true)) .ToList();
Regards,
- Edited by Yan Grenier - MTFC Friday, February 27, 2015 11:39 AM
- Marked as answer by wapt49 Monday, March 2, 2015 1:31 PM
-
I'm curious why you find your code is not clean. It is easy to read for every beginning programmer and therefore easy to maintain.
It is not slow because in Linq the same is done behind the scene.
Using Expressions was created for those who had difficulties with programming and therefore wanted more expressions.
With this I don't try to tell to avoid Linq, but I find it always crazy that persons think they must for a strange reason use Linq.
Success
Cor -
Hi all,
Thanks all for the answer.
@Cor
Yes, maybe its wrong too say "not clean". As you said, its easier to debug using foreach than using LINQ, However I personally think using LINQ is more eleganter (the code can be smaller) than using nested foreach :)
-
I don't like using a compound condition like below. I don't trust compilers (had lot of issues). If the compiler doesn't create code from left to right, an exception can occur when x.EmergencyEvents is null.
.Where(x => x.EmergencyEvents != null && x.EmergencyEvents.Any(z => z.IsEmergencyDrill == true))
jdweng
-
Why the compiler whould not create the code from left to right ?
It's in the C# Specification : https://msdn.microsoft.com/en-us/library/aa691310(v=vs.71).aspx .
There is a case when it's not right ?
Yan Grenier
Merci de bien vouloir "Marquer comme réponse", les réponses qui ont répondues à votre question, et de noter les réponses que vous avez trouvé utiles.