简体   繁体   English

将匿名集合放入类中

[英]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. 所以我想要一个具有3个int属性的类,包括'Total','Page'和'Count',然后是一个属性,它是一个集合。 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> 错误205无法将类型System.Collections.Generic.List<AnonymousType#1>隐式转换为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. 如何获得一个具有3个int属性的类和一个接受匿名类型的Collection属性。

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. 然后你应该使你的类型通用,类似于lazyberezovsky在他的答案中建议的。 You need to take advantage of type inference, which doesn't work on constructors. 您需要利用类型推断,这对构造函数不起作用。 But you can use a static factory method: 但您可以使用static工厂方法:

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 . 如果您不想这样做,您可以使用协变接口,如IEnumerable (或IReadOnlyList如果您使用.Net 4.5)而不是List You can't cast List<AnonymousType> to List<object> (or IList<object> ), because that wouldn't be safe. 您不能将List<AnonymousType>List<object> (或IList<object> ),因为这样做不安全。 But it's safe to cast it to IEnumerable<object> . 但将它IEnumerable<object>IEnumerable<object>是安全的。

One more option would be to create List<object> containing objects of the anonymous type in the first place. 还有一个选项是首先创建包含匿名类型对象的List<object> To do that, specify the type when calling ToList : .ToList<object>() instead of .ToList() . 为此,请在调用ToList时指定类型: .ToList<object>()而不是.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. jsonData.rows现在将是一个对象列表,而不是匿名类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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