I have the follow method that returns a dynamic object representing an IEnumerable<'a>
('a=anonymous type) :
public dynamic GetReportFilesbyStoreProductID(int StoreProductID)
{
Report report = this.repository.GetReportByStoreProductID(StoreProductID);
if (report == null || report.ReportFiles == null)
{
return null;
}
var query = from x in report.ReportFiles
orderby x.DisplayOrder
select new { ID = x.RptFileID, Description = x.LinkDescription, File = x.LinkPath, GroupDescription = x.ReportFileGroup.Description };
return query;
}
I want to be able to access the Count
property of this IEnumerable
anonymous type. I'm trying to access the above method using the following code and it is failing:
dynamic Segments = Top20Controller.GetReportFilesbyStoreProductID(StoreProductID");
if (Segments.Count == 0) // <== Fails because object doesn't contain count.
{
...
}
- How does
dynamic
keyword operate? - How can I access the
Count
property of theIEnumerable
anonymous type? - Is there a way I can use this anonymous type or do I have to create a custom object so that I can pass back a strongly-typed
IEnumerable<myObject>
instead ofdynamic
?
I'd prefer to not do that if I can as this method is only called in one place and creating a throw-away object seems like overkill.