JimmyB JimmyB - 22 days ago 6
C# Question

Extension method on Item in List within another List

I'm trying to perform an run an extension method on every item within a

List<>
that is inside another
List<>
to return collection of a given type (returned by the extension method).

I initially tried (and failed) to do this using linq, but I have the following:

var dataset = GetReportDataset(org);

var reportData = new List<InterventionAndNeetRiskReportLineModel>();

foreach (var record in dataset)
{
foreach (var inter in record.InterventionHistory)
{
reportData.Add(inter.ToInterventionAndNeetRiskReportLineModel());
}
}
return _reportWriter.ReportCsvStream(reportData);


So my question is, how can I project the result of my extension method for each item in the child collection using linq?

UPDATE
ToInterventionAndNeetRiskReportLineModel()
extension method

public static InterventionAndNeetRiskReportLineModel ToInterventionAndNeetRiskReportLineModel(this InterventionHistory intervention)
{
return new InterventionAndNeetRiskReportLineModel()
{
Beneficiary = intervention.Person.Beneficiary,
CourseTitle = intervention.CourseTitle,
CaseNotes = intervention.CaseNotes,
EndDate = intervention.EndDate?.ToString(),
StartDate = intervention.StartDate.ToString(),
ParticipantId = intervention.Person.ParticipantId,
FirstEit = intervention.Person.EitScores.GetFirstEitReading().ToString(),
LastEit = intervention.Person.EitScores.GetLastEitReading().ToString(),
FirstLpt = intervention.Person.LptScores.GetFirstLptReading().ToString(),
LastLpt = intervention.Person.LptScores.GetLastLptReading().ToString(),
Gender = intervention.Person.Equalitites.Gender,
HoursAttended = intervention.NoOfHours.ToString(),
LanguageOfDelivery = intervention.DeliveryLanguage,
Providername = intervention.ProviderName,
QanCode = intervention.QanCode,
SchoolCollegeName = intervention.ProviderName
};
}

Answer

I'm not completely sure which part of the question code you want to separate into an extension method. Also, don't be to focused on the extension method part, its nothing different from other functions, as far as the writing is concerned.

You can use SelectMany in order to get a flat list of InterventionHistory objects and Select in order to convert to InterventionAndNeetRiskReportLineModel and ToList for the final result as list instead of IEnumerable<T> if you really need that.

var reportData = GetReportDataset(org)
    .SelectMany(r => r.InterventionHistory)
    .Select(i => i.ToInterventionAndNeetRiskReportLineModel())
    .ToList();

So, maybe you want an extension method like

public static IEnumerable<InterventionAndNeetRiskReportLineModel> ToInterventionRiskReports(this IEnumerable<ReportDataset> _self)
     return _self
        .SelectMany(r => r.InterventionHistory)
        .Select(i => i.ToInterventionAndNeetRiskReportLineModel());
}

And use it as

var reportData = GetReportDataset(org).ToInterventionRiskReports().ToList();

... as I said, its not completely clear, which part you want to refactor into an extension method.