Advent Challenge #8

Hi guys! @abu.behlim and @monsieurrahul I have made a tutorial for the Advent Challenge #7 with LINQ here: Filter data easily and invoke custom code | UiPath Advent Challenge 2020 #7 | UiPath Tutorial - YouTube. I will have more content like this, so do the goodies with Subscribe and Like :slight_smile:

While I resolved the #8 challenge already I am posting the solution here and I will publish the video explained after Christmas

Advanced solution:

I think this is the shortest way to resolve this challenge :slight_smile:

LINQ:
var isDefinedTaskQuery = from executionTask in executions.AsEnumerable().Select(li => li.Field(“TASK”))
select new
{
Task = executionTask,
IsDefined = tasks.AsEnumerable().Select(li => li.Field(“Task”)).Contains(executionTask)
};

var taskIsStartedQuery = from taskRow in tasks.AsEnumerable() 
	                                     let task = taskRow.Field<string>("Task")
										select new 
										{
											Task = task,
											IsStarted = executions.AsEnumerable().Select(li => li.Field<string>("TASK")).Contains(task)
										};

var hasValidRCExcutedQuery = from executionRow in executions.AsEnumerable()
	                                               let task = executionRow.Field<string>("TASK")
											       let definedRegionCodes = tasks.AsEnumerable()
																									.Where(li => li.Field<string>("Task") == task)
																									.SelectMany(li => li.Field<string>("RegionCode").Split(new Char[] { ',' }))
	                                              select new 
					 				         	{
											         Task = task,
												  	 HasValidRCExcuted = definedRegionCodes.Contains(executionRow.Field<string>("REGIONCODE"))
												};
var completedAllValidRCsQuery = from taskRow in tasks.AsEnumerable()
			                                            let task = taskRow.Field<string>("Task")
			                                            let regionCodes = taskRow.Field<string>("RegionCode").Split(new char[] { ',' })
			                                            let completed = from executionRow in executions.AsEnumerable()
					                                                              where executionRow.Field<string>("RESULT") == "OK" && executionRow.Field<string>("TASK") == task
					                                                              select executionRow.Field<string>("REGIONCODE")
			                                            select new
			                                            {
			                                                Task = task,
			                                                CompletedAllValidRCs = regionCodes.All(li => completed.Contains(li))
			                                            };
												  
var query = from task in tasks.AsEnumerable().Select(li => li.Field<string>("Task")).Union(executions.AsEnumerable().Select(li => li.Field<string>("TASK"))).Distinct()
			       orderby task
				   select new 
				   {
					     Task = task,
					     IsDefinedTask = isDefinedTaskQuery.Any(li => li.Task == task && li.IsDefined),
					     TaskIsStarted = taskIsStartedQuery.Any(li => li.Task == task && li.IsStarted),
					     ValidRCExecuted = hasValidRCExcutedQuery.Any(li => li.Task == task && li.HasValidRCExcuted),
					     HasValidRCExcuted = completedAllValidRCsQuery.Any(li => li.Task == task && li.CompletedAllValidRCs)
				   };
				   
foreach (var task in query)
	resultDataTable.Rows.Add(new Object[] { task.Task, task.IsDefinedTask, task.TaskIsStarted, task.ValidRCExecuted, task.HasValidRCExcuted });

This could be done in fewer lines but It would been way hard to understand! Cheers!

5 Likes