匿名类型集合? [英] Anonymous type collections?

查看:209
本文介绍了匿名类型集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找用于创建匿名类型集合的最佳做法。



有几种方法 - 一个和大多数答案这个线程假设整个匿名集合可以在一个语句中构造。 p>

由于匿名类型通常用于替换用于存储临时的类(如 =http://blogs.msdn.com/timng/archive/2007/04/07/making-collections-of-anonymous-types.aspx =nofollow noreferrer>此帖子建议。 / p>

在我的例子中,我迭代一个集合 - 对于集合中的每个项目,我想收集一个匿名类型的相关对象(作为一个元组)。



我已经考虑过的其他方法:


$ b。我需要这些匿名类型才能放入一个集合, $ b


  1. 使用对象[]或ArrayList(失去匿名类型的好处)

  2. 为所需项创建包装类需要匿名类型)

这是我的代码中唯一需要这样一个集合的地方 - 什么是最好的方法?



其他信息:



,所以我正在使用的集合是强类型集合实现IEnumerable,因此大多数Linq扩展方法,如.Select将不工作:



代码

  foreach(myItems中的项目项目)
{
foreach item.GetConfirmations(true))
{
if(lastParentId!= item.ParentId)
{
lastParentId = item.ParentId;
parentObject = new ParentItem(lastParentId);
}

itemHolder = new ArrayList {item,confirmation};
if(parentObject!= null)
{
itemHolder.Add(parentObject.Rate);
}

sortConfirmations.Add(confirmation.Date,itemHolder);
}
}

//使用sortConfirmations

解决方案



我最终使用通用字典来收集相关项目,并使用.OrderBy()排序 - 丑陋,

解决方案

对于一个非常不同的答案... LINQ?


在我的例子中,我迭代一个
集合 - 对于
集合中的每个项目,我想收集
匿名类型
中的相关对象(作为元组)。我需要这些
匿名类型放在
集合,然后排序他们为
进一步工作。



$ b b

这听起来像:

  var list =(从集合中的项目
从相关的item.Relationship
order by ... something ...
select new {item,related})ToList()


I am looking for best practices for creating collections made from anonymous types.

There are several approaches - this one and most answers on this thread assume that the whole anonymous collection can be constructed in one statement.

As anonymous types are usually used to replace classes that are to be used to store temporary (as put forward in this SO answer), I would like to avoid creating and using a class for anonymous collections, as this post suggests.

In my case, I am iterating over a collection - for each item in the collection I would like to collect related objects in an anonymous type (acting as a tuple). I need these anonymous types to be put in a collection and then sort them for further work.

Other approaches I have considered:

  1. Use an object[] or ArrayList (losing the benefit of the anonymous type)
  2. Creating a wrapper class for the required items (removing the need for the anonymous type)

This is the only place in my code that I need such a collection - what would be the best approach?

Additional Information:

I am working with legacy .Net 1.1 objects, so the collections I am working with are strongly typed collections implementing IEnumerable, and as such most Linq extension methods such as .Select won't work:

Code sample

foreach (Item item in myItems)
{
    foreach (Confirmation confirmation in item.GetConfirmations(true))
    {
        if (lastParentId != item.ParentId)
        {
            lastParentId = item.ParentId;
            parentObject = new ParentItem(lastParentId);
        }

        itemHolder = new ArrayList { item, confirmation };
        if (parentObject!= null)
        {
            itemHolder.Add(parentObject.Rate);
        }

        sortConfirmations.Add(confirmation.Date, itemHolder);
    }
}

// Use sortConfirmations

Resolution

I ended up using a generic dictionary to collect the related items together and using .OrderBy() to sort them - ugly, but it works... and is better than the existing code.

解决方案

For a very different answer... LINQ?

In my case, I am iterating over a collection - for each item in the collection I would like to collect related objects in an anonymous type (acting as a tuple). I need these anonymous types to be put in a collection and then sort them for further work.

That sounds like:

var list = (from item in collection
          from related in item.Relationship
          order by ...something...
          select new {item,related}).ToList()

这篇关于匿名类型集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆