linq 分组上的克隆对象 - 复制构造函数替代
我有一组自定义对象(资产),我想将它们与 LINQ 分组。 自定义对象具有标准属性,如 id、名称和成本属性。 分组时,我想计算每个组的成本,所以我使用这样的小技巧:
from a in assets
group a by a.AssetId into ga
select new Asset()
{
AssetId = ga.Key,
Cost = ga.Select(gg=>gg.Cost).Sum()
}
好的,这里一切都很好。但是...为了初始化订单属性,我一起使用复制构造函数和成本计算...
from a in assets
group a by a.AssetId into ga
select new Asset(ga.FirstOrDefault())
{
AssetId = ga.Key,
Cost = ga.Select(gg=>gg.Cost).Sum()
}
所以现在,我按 id 获取分组资产的集合,其中所有属性都是从组中的第一个资产复制的计算分组成本。但是......为了做到这一点,我需要为使用这种分组的每个对象编写一个带有“所有属性初始化”的复制构造函数,在我的例子中这是开销,因为有超过 20 个属性的对象。
我尝试使用链接中的克隆技巧:
in linq group query但是没有成功。
我的问题:有没有更好/更优雅的方法来实现这一点?
谢谢
I have collection of custom objects(assets) which I want to group with LINQ.
Custom object has standard properties like id, name and cost property.
When grouping I want to calculate cost for each group, so I'm using little trick like this:
from a in assets
group a by a.AssetId into ga
select new Asset()
{
AssetId = ga.Key,
Cost = ga.Select(gg=>gg.Cost).Sum()
}
Ok, everything is fine here. But...in order to initialize order properties as well, I'm using copy contructor and cost calculations together...
from a in assets
group a by a.AssetId into ga
select new Asset(ga.FirstOrDefault())
{
AssetId = ga.Key,
Cost = ga.Select(gg=>gg.Cost).Sum()
}
So now, I get collection of grouped assets by id, with all properties copied from a first asset in a group with calculated grouped cost. But...in order to do that I need to write for every object that using this kind of grouping, a copy constructor with 'all properties initialization' which in my case is overhead because there are objects with 20+ properties.
I've tried to use clone trick from a link:
in linq group query but with no success.
My question: is there a better/more elegant way to accomplish this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不需要深层复制它们,您可以使用 MemberwiseClone 方法。实现(来自 Object 类)&给你一个浅拷贝。所以行
new Asset(ga.FirstOrDefault())
将变为
((Asset)(ga.FirstOrDefault().MemberwiseClone()))
。但是,作为浅复制,[First Asset].SomeObject 将指向 [Grouped Asset].SomeObject,并且其中一个资源的更改将反映在另一个资源上。
编辑以包括注释中描述的辅助方法:
现在,您可以使用该方法如下:
If you don't need deep copy them you can use MemberwiseClone method. The implementation (comes from Object class) & gives you a shallow copy. So line
new Asset(ga.FirstOrDefault())
will become
((Asset)(ga.FirstOrDefault().MemberwiseClone()))
.However, being shallow copy, [First Asset].SomeObject would point to [Grouped Asset].SomeObject and changes from one would reflect at other.
Edited to include helper method described in comments:
Now, you may use the method as follows: