如何在LINQ中将匿名类型转换为强类型? [英] How can I convert anonymous type to strong type in LINQ?

查看:104
本文介绍了如何在LINQ中将匿名类型转换为强类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListViewItems数组(ListViewItem[]),在其中将SalesOrderMaster对象存储在每个ListViewItem.Tag中,以供以后参考.

I have an array of ListViewItems ( ListViewItem[] ), where I store a SalesOrderMaster object in each ListViewItem.Tag for later reference.

我现在有一些代码,遍历每个ListViewItem,将.Tag属性安全地强制转换为SalesOrderMaster对象,然后将该对象添加到SalesOrders的集合中,仅在确保确定该订单尚未完成之后才进行存在于那个集合中.

I have some code that right now, goes through each ListViewItem safely casts the .Tag property into a SalesOrderMaster object, then adds that object to a collection of SalesOrders, only after checking to make sure the order doesn't already exist in that collection.

比较销售订单的过程非常昂贵,我想将其转换为LINQ表达式以提高清晰度和性能. (我也有并行扩展到安装的.NET Framework 3.5 ,这样我就可以用来进一步提高LINQ性能)

The process to compare sales orders is expensive, and I would like to convert this to a LINQ expression for clarity and performance. ( I also have the Parallel Extensions to .NET Framework 3.5 installed so I can use that to further improve LINQ performance)

因此,事不宜迟:这就是我所拥有的,然后是我想要的. (我想要的东西不会编译,所以我知道我做错了,但我希望它能说明问题所在).

So without further ado: This is what I have, and then what I want. ( what I want won't compile, so I know I am doing something wrong, but I hope it illustrates the point )

我所拥有的:(慢)

foreach (ListViewItem item in e.Argument as ListViewItem[])
            {
                SalesOrderMaster order = item.Tag as SalesOrderMaster;
                if ( order == null )
                {
                    return;
                }
                if (!All_SalesOrders.Contains(order))
                {
                    All_SalesOrders.Add(order);
                }
            }

我想要的是:(理论)

    List<SalesOrderMaster> orders = 
(from item in (e.Argument as ListViewItem[]).AsParallel() 
select new { ((SalesOrderMaster)item.Tag) }).Distinct();

我知道演员表很便宜,我说了比较",在这种情况下,它翻译为.Contains(order)操作

I know the cast is cheap, I said the "Compare", which in this case translates to the .Contains(order) operation

每个人的回答都很棒!我希望可以标记多个答案,但最后我必须选择一个答案.

Everyone's answer was awesome! I wish I could mark more than one answer, but in the end I have to pick one.

这就是我最终得到的:

List<SalesOrderMaster> orders = 
(from item in (e.Argument as ListViewItem[]) select (SalesOrderMaster) item.Tag).GroupBy(item => item.Number).Select(x => x.First()).ToList();

推荐答案

我看不到有人解决过将匿名类型显式转换为命名类型的需求,因此这里...通过使用"select new { }",您可以创建一个匿名类型,但您不必这样做.您可以这样编写查询:

I see nobody has addressed your need to convert an anonymous type to a named type explicitly, so here goes... By using "select new { }" you are creating an anonymous type, but you don't need to. You can write your query like this:

List<SalesOrderMaster> orders = 
    (from item in (e.Argument as ListViewItem[]).AsParallel() 
    select (SalesOrderMaster)item.Tag)
    .Distinct()
    .ToList();

请注意,查询选择的是不带new { }(SalesOrderMaster)item.Tag,因此它不会创建匿名类型.还要注意,我添加了ToList(),因为您想要一个List<SalesOrderMaster>.

Notice that the query selects (SalesOrderMaster)item.Tag without new { }, so it doesn't create an anonymous type. Also note I added ToList() since you want a List<SalesOrderMaster>.

这解决了您的匿名类型问题.但是,我同意Mark和Guffa的观点,在这里使用并行查询并不是您的最佳选择.要按照Guffa的建议使用HashSet<SalesOrderMaster>,您可以执行以下操作:

This solves your anonymous type problem. However, I agree with Mark and Guffa that using a parallel query here isn't you best option. To use HashSet<SalesOrderMaster> as Guffa suggested, you can do this:

IEnumerable<SalesOrderMaster> query = 
    from item in (ListViewItem[])e.Argument
    select (SalesOrderMaster)item.Tag;

HashSet<SalesOrderMaster> orders = new HashSet<SalesOrderMaster>(query);

(我避免使用var,因此返回的类型在示例中清晰可见.)

(I avoided using var so the returned types are clear in the examples.)

这篇关于如何在LINQ中将匿名类型转换为强类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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