Reputation: 892
I'm writing a .Net 3.5 C# script for Sony Vegas Pro. I've created this method which returns true if it finds a video event with the same timecode as a given audio event.
This works but I'm wondering if I could get rid of the foreach loops using LINQ. I'm just learning LINQ and I'm finding it quite hard so if you could explain your thought steps in getting to the solution, that'd be great.
private bool videoExistsWithSameStart(TrackEvent currentEvent)
{
//Look through each video track - we don't want audio tracks.
var videoTracks = from t in myVegas.Project.Tracks
where t.IsVideo()
select t;
foreach (Track t in videoTracks)
{
foreach (TrackEvent te in t.Events)
{
if (te.Start == currentEvent.Start)
return true;
}
}
return false;
}
Upvotes: 1
Views: 1398
Reputation: 38179
return videoTracks.Any(track =>
track.Events.Any(pumpkin =>
pumpkin.Start == currentEvent.Start));
Upvotes: 1
Reputation: 3194
Basically what's easiest to do (for me at least) is trying to spell out what you're trying to do:
return videoTracks.Any(track => track.Events.Any(event => event.Start == currentEvent.Start));
What we want to know is does videoTracks have Any tracks where Any of its Events compare to the current one.
Upvotes: 0
Reputation: 93060
return videoTracks.Any(t => t.Events.Any(te => te.Start == currentEvent.Start));
Upvotes: 0
Reputation: 23949
Forgive me, I don't have access to Visual Studio right now, but care to try?
return myVegas.Projects.Tracks.Any(t => t.IsVideo() && t.Events.Any(e => e.Start == currentEvent.Start));
Upvotes: 0
Reputation: 25638
Another alternative:
return myVegas.Project.Tracks
.Where(track => track.IsVideo())
.SelectMany(track => track.Events)
.Any(trackEvent => trackEvent.Start == currentEvent.Start);
Upvotes: 3