Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access count property of a dynamic type in C# 4.0?
    primarykey
    data
    text
    <p>I have the follow method that returns a dynamic object representing an <code>IEnumerable&lt;'a&gt;</code> ('a=anonymous type) :</p> <pre><code> 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; } </code></pre> <p>I want to be able to access the <code>Count</code> property of this <code>IEnumerable</code> anonymous type. I'm trying to access the above method using the following code and it is failing:</p> <pre><code> dynamic Segments = Top20Controller.GetReportFilesbyStoreProductID(StoreProductID"); if (Segments.Count == 0) // &lt;== Fails because object doesn't contain count. { ... } </code></pre> <ul> <li>How does <code>dynamic</code> keyword operate?</li> <li>How can I access the <code>Count</code> property of the <code>IEnumerable</code> anonymous type?</li> <li>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 <code>IEnumerable&lt;myObject&gt;</code> instead of <code>dynamic</code>? </li> </ul> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload