none
Generic type to use for result sets RRS feed

  • Question

  • I have a Reporting module with a base class ReportBase to use for generating reports

     

    I have a virtual function GetReportData() which will run the report, but having trouble in the return type to use. I needed a result type that I can still page on with .Skip() and .Take() and also count the length using .Count

     

    I figured that IQueryable<Object> would be appropriate since instances of reports would be returning LINQ queries, and I don't know the result type in ReportBase.

     

    However, any LINQ query seems to return some System.Data.Linq.DataQuery'1 generic type and I cannot cast it using .AsQueryable or .AsEnumberable

     

    Any suggestions?

    Tuesday, August 19, 2008 5:22 PM
    Answerer

Answers

  • When you call GetReportData do you know what type you will get back? If so you could make it a generic method.

    The other alternative is to write an IEnumerable wrapper that converts the results types within a yield. This is required because you can't cast a generic of one type into a generic of other (consider converting List<Object> to List<String>... whilst string is a subtype of object the List<String> would not accept Objects and so isn't substitutable)

    I have an example of code to convert IEnumerable<T> to IEnumerable<Subclass of T> at my blog http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft

    [)amien
    Tuesday, August 19, 2008 5:36 PM
    Moderator

All replies

  • When you call GetReportData do you know what type you will get back? If so you could make it a generic method.

    The other alternative is to write an IEnumerable wrapper that converts the results types within a yield. This is required because you can't cast a generic of one type into a generic of other (consider converting List<Object> to List<String>... whilst string is a subtype of object the List<String> would not accept Objects and so isn't substitutable)

    I have an example of code to convert IEnumerable<T> to IEnumerable<Subclass of T> at my blog http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft

    [)amien
    Tuesday, August 19, 2008 5:36 PM
    Moderator
  • Thanks for the very clear reply.

     

    I've decided to just return a simple IEnumerable result and let the report system handle it this way - it doesn't care about the contained type (it's designed to expect objects and use reflection to access properties by name). Worked straight away..

     

    Wednesday, August 20, 2008 8:07 AM
    Answerer
  • I think I've also figured out another option relating to the article you mentioned: it is possible to downgrade to a base class using .Cast<>

     

    e.g. if somedata is type IEnumerable<T> and Tbase is a base class of T then somedata.Cast<T2> will return the conversion

    Wednesday, August 20, 2008 8:16 AM
    Answerer
  • Yes, you're right .NET 3.5 added that Cast extension method to achieve the same thing (that blog post was written back before 3.5)

    [)amien
    Wednesday, August 20, 2008 8:29 AM
    Moderator