简体   繁体   中英

Putting an Anonymous collection into a Class

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()
};

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();

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() .

The other answers that suggest a generic type can definitely make sense, but since you are passing this data to another class that must be reflecting into the class anyways you could use a list of objects.

The do this you need to cast the anonymous instances as part of your query, like so:

var jsonData = new {
      total = totalPages,
      page = gridSettings.PageIndex,
      records = counter,
      rows = (from c in ucs select new {id = c.id, createDate =         
        c.CreateDate}).Cast<Object>().ToList()
};

jsonData.rows will now be a list of objects, instead of the anonymous type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM