switch(ID)
{
case "CustomReportsContainer":
foreach (var report in SessionRepository.Instance.CustomReports.ToList())
{
var reportItem = new RadListBoxItem(report.Name, report.ID.ToString());
if (!Items.Any(item => int.Equals(item.Value, reportItem.Value)))
{
Items.Add(reportItem);
}
}
break;
case "HistoricalReportsContainer":
foreach (var report in SessionRepository.Instance.HistoricalReports.ToList())
{
var reportItem = new RadListBoxItem(report.Name, report.ID.ToString());
if (!Items.Any(item => int.Equals(item.Value, reportItem.Value)))
{
Items.Add(reportItem);
}
}
break;
}
HistoricalReports and CustomReports are collections of different types, but I am interested in the same two properties from each object type. I thought that I should be able to use LINQ’s Select, and create a list of anonymously typed objects.
I can’t create an implicitly typed variable without assigning to it, though. And since I’m inside of a Switch statement’s scope… I can’t assign to var inside of the switch statement, then move the rest of the code outside of the switch statement.
How should I express this code? Is the current implementantion ‘best’?
Uggh, close. Can I do anything with this? It’s through our API so I can’t modify any deeper.
ReportServices.GetAllCustomReports().Select(report => new { report.Name, report.ID} ).ToList().ForEach(customReport => _customReports.Add(customReport));
Error 2 Argument 1: cannot convert from 'AnonymousType#1' to 'CableSolve.Web.Dashboard.IReport'
CustomReportandHistoricalReportshould implement anIReportinterface that contains theIdandNameproperties (at minimum).Edit
In response to your additional questions, would it be possible for
GetAllCustomReportsto return a type that implements theIReportinterface? This would eliminate the need for projecting to an anonymous type viaSelect(report => new { report.Name, report.ID} )and should resolve the remaining issue.