I may not being using the correct term "nested", but you can see the 2 classes I have below. IEnumerable<OperationTask>
is in the IEnumerable<AxApp>
.
I hope I can explain this correctly:
I have a method that is reading values from a txt file into a string[]
string[] appNames = _appNamesRepository.GetAppNameListFromInputFile(fileName);
Then I create an IEnumerable<AxApp>
collection with a method that reads a SQL table and returns the AxApp properties for each app value in the txt file.
IEnumerable<AxApp> allAxApps = _axAppRepository.GetAllAxAppsInList(appNames);
I need to filter the IEnumerable<AxApp>
collection based on specific criteria from values in the IEnumerable<OperationTask>
. I can get a basic lambda search to work, but I can't return the correct result if I have more than a couple values I'm basing this on.
For example the below will only return an IEnumerable<AxApp>
if the appid, operationType, and operationStatus.Incomplete are NOT in IEnumerable<OperationTasks>
.
var test = allAxApps.Where(app =>
!app.operationTasks
.Any(task => task.appId == app.appid &&
task.operationType == operationType &&
task.operationStatus != Status.Incomplete));
HOWEVER, I need to go a step further, if the operationType == "Differential", I can only return the AxApp IF IN IEnmerable<OperationTask>
there is a collection where type = "NewConversion", status = "Complete", but NO type = "Finish", but can include types = "Differential"
public class AxApp
{
public AxApp()
{
}
public AxApp(int id, string appname, string dlname)
{
this.appname = appname;
appid = id;
this.dlname = dlname;
}
public string appname { get; set; }
public int appid { get; set; }
public string dlname { get; set; }
public string dtname { get; set; }
public int flags { get; set; }
public IEnumerable<AxDlsd> dlsdRecords { get; set; }
public IEnumerable<AxDl> dlRecords { get; set; }
public IEnumerable<OperationTask> operationTasks { get; set; }
public DateTime startMerge { get; set; }
public DateTime endMerge { get; set; }
}
public class OperationTask
{
public int operationId { get; set; }
public int appId { get; set; }
public OperationType operationType { get; set; }
public Status operationStatus { get; set; }
public DateTime startTime { get; set; }
public DateTime endTime { get; set; }
}
Method that returns the AppData from any value in the string[]
public IEnumerable<AxApp> GetAllAxAppsInList(string[] appNamesInput)
{
string query = @"SELECT appid, appname, dlname, dtname, flags FROM dbo.ae_apps WHERE appname = @_appname";
string opQuery = @"SELECT operationId, appId, operationType, operationStatus, startTime, endTime FROM dbo.RDS_ConversionOperationsHistory WHERE appid = @_appId";
using (var connection = _dbConnectionFactory.GetAxDbConnection())
{
foreach (string appname in appNamesInput)
{
AxApp result = connection.QuerySingle<AxApp>(query, new { _appname = appname });
result.operationTasks = connection.Query<OperationTask>(opQuery, new { _appId = result.appid });
yield return result;
}
}
}
IEnumerable for AxApp/OperationTask
[0] = {RDS.Conversion.UtilityLibrary.Models.AxApp}
[0] {RDS.Conversion.UtilityLibrary.Models.AxApp} RDS.Conversion.UtilityLibrary.Models.AxApp
appid 1 int
appname "PLIC_CENT_1" string
dlRecords null System.Collections.Generic.IEnumerable<RDS.Conversion.UtilityLibrary.Models.AxDl
dlname "ae_dlsd1_SIM" string
dlsdRecords null System.Collections.Generic.IEnumerable<RDS.Conversion.UtilityLibrary.Models.AxDlsd>
dtname "ae_dt1" string
endMerge {1/1/0001 12:00:00 AM} System.DateTime
flags 16384 int
operationTasks Count = 3 System.Collections.Generic.IEnumerable<RDS.Conversion.UtilityLibrary.Models.OperationTask> {System.Collections.Generic.List<RDS.Conversion.UtilityLibrary.Models.OperationTask>}
startMerge {1/1/0001 12:00:00 AM} System.DateTime
operationTasks Count = 3 System.Collections.Generic.IEnumerable<RDS.Conversion.UtilityLibrary.Models.OperationTask> {System.Collections.Generic.List<RDS.Conversion.UtilityLibrary.Models.OperationTask>}
[0] {RDS.Conversion.UtilityLibrary.Models.OperationTask} RDS.Conversion.UtilityLibrary.Models.OperationTask
appId 1 int
endTime {1/1/0001 12:00:00 AM} System.DateTime
operationId 18 int
operationStatus Complete RDS.Conversion.UtilityLibrary.Models.Status
operationType NewConversion RDS.Conversion.UtilityLibrary.Models.OperationType
startTime {9/28/2018 12:53:51 PM} System.DateTime
I'm not sure if this is what you're looking for but, it may get you in the right direction.
test = allAxApps.Where(app =>
{
var tasks = app.operationTasks.ToList();
var differentialCondition = false;
if (operationType == OperationType.Differential)
{
var hasCompletedNewConversion = tasks.Any(task =>
task.operationType == OperationType.NewConversion &&
task.operationStatus == Status.Complete);
var hasFinish = tasks.Any(task => task.operationType == OperationType.Finish);
differentialCondition = hasCompletedNewConversion && !hasFinish;
}
var hasIncomplete = tasks.Any(task =>
task.appId == app.appid &&
task.operationType == operationType &&
task.operationStatus == Status.Incomplete);
return differentialCondition && !hasIncomplete;
});