Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting an Anonymous collection into a Class

Tags:

c#

linq

generics

I have a link query which returns different lists of data to fill different Grids on webpages, but it always returns some standard properties as well as a collection of anonymous objects.

so I want a class which has 3 int properties for 'Total', 'Page', and 'Count' then a property which is a collection. I still want the property to change so I can use this object to return multiple types of queries. I know I could 'hard-type' every query but there are so many variations of queries that it would be best to have a generic type of class which would accept an Anonymous collection.

I have tried using a property of

List<object> myQueryCollection

but get the error:

Error 205 Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to System.Collections.Generic.List<object>

How can I get a class which has 3 int properties and a Collection property which accepts a anonymous type.

I want to pass a proper object from my biz layer to my views...

var jsonData = new {
      total = totalPages,
      page = gridSettings.PageIndex,
      records = counter,
      rows = (from c in ucs select new {id = c.id, createDate =         
        c.CreateDate}).ToList()
};
like image 980
matthewbaskey Avatar asked Dec 11 '25 09:12

matthewbaskey


2 Answers

You can create generic class

public class Result<T>
{
    public int Total { get; set; }
    public int Page { get; set; }
    public int Count { get { return Items.Count; } }
    public List<T> Items { get; set; }
}

And extension method to create instance of this class parametrized with your anonymous type:

public static Result<T> ToResult<T>(this List<T> source)
{
    return new Result<T> { Items = source };
}

Usage:

var result = yourList.ToResult();
like image 131
Sergey Berezovskiy Avatar answered Dec 13 '25 21:12

Sergey Berezovskiy


it would be best to have a generic type of class which would accept an Anonymous collection

Then you should make your type generic, similar to what lazyberezovsky suggested in his answer. You need to take advantage of type inference, which doesn't work on constructors. But you can use a static factory method:

public static class ResultFactory
{
    public static Result<T> Create<T>(int total, int page, List<T> items)
    {
        return new Result<T> { Total = total, Page = page, Items = items };
    }
}

Usage:

var data = ResultFactory.Create(
      totalPages,
      gridSettings.PageIndex,
      (from c in ucs select new { c.id, createDate = c.CreateDate }).ToList());

If you don't want to do that, you could use a covariant interface like IEnumerable (or IReadOnlyList if you're on .Net 4.5) instead of List. You can't cast List<AnonymousType> to List<object> (or IList<object>), because that wouldn't be safe. But it's safe to cast it to IEnumerable<object>.

One more option would be to create List<object> containing objects of the anonymous type in the first place. To do that, specify the type when calling ToList: .ToList<object>() instead of .ToList().

like image 29
svick Avatar answered Dec 13 '25 21:12

svick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!