从继承自 List的自定义集合返回内部列表;
我创建了一个继承自 List 的自定义集合,我创建了一个对我的数据执行一些操作的方法。我所有的操作都是在集合内的私有字段中完成的。
我已将 Private 字段设置为引用 this 对象,然后我需要对数据进行排序,以便可以进行计算。我使用 Linq 进行此操作,认为 *this 和我的 Private Field 是相同的。但使用 Linq 进行的更改不会更改 this 对象。虽然我所做的每一个其他改变确实改变了它。
class OrdersDetailCollection : List<OrdersDetail>
{
List<OrdersDetail> _List;
public OrdersDetailCollection(IEnumerable<OrdersDetail> input)
{
this.AddRange(input);
_List = this;
_List = (from l in _List
orderby l.Item, l.DateExp
select l).ToList();
}
public void CalculateQty()
{
RemoveDuplicate();
_List = (from l in _List
where double.Parse(l.QteBO) > 0
orderby l.DateExp, l.Item
select l).ToList();
}
private void RemoveDuplicate()
{
//Do some stuff to _List
}
this 和 _List 不应该相同吗,因为它们引用了内存中的相同空间?如何将 _List 分配给它?
我的自定义集合缺少什么,以便我可以像列表一样使用它并执行以下操作(简化)?
OrdersDetailCollection ShipList = new OrdersDetailCollection(/*Value from SQL*/)
ShipList.CalculateQty();
return ShipList;
我想知道如何在没有扩展方法的情况下做到这一点,我认为产量可能与它有关,但不太理解它。
I have made a custom collection that inherits from List, I have created a methods that does some work to my data. All my manipulation is done from a private field inside the Collection.
I have set my Private field to reference the this object, I then need to sort my data so I can do my calculation. I do it with Linq, thinking that *this and my Private Field are the same. But changes made with Linq does not change the this object. While every other change I made does change it.
class OrdersDetailCollection : List<OrdersDetail>
{
List<OrdersDetail> _List;
public OrdersDetailCollection(IEnumerable<OrdersDetail> input)
{
this.AddRange(input);
_List = this;
_List = (from l in _List
orderby l.Item, l.DateExp
select l).ToList();
}
public void CalculateQty()
{
RemoveDuplicate();
_List = (from l in _List
where double.Parse(l.QteBO) > 0
orderby l.DateExp, l.Item
select l).ToList();
}
private void RemoveDuplicate()
{
//Do some stuff to _List
}
Shouldn't this and _List be the same since they reference the same space in memory? How to assign _List to this?
What is missing from with my Custom Collection so that I can use it the same way as a List and do the following (simplified)?
OrdersDetailCollection ShipList = new OrdersDetailCollection(/*Value from SQL*/)
ShipList.CalculateQty();
return ShipList;
I want to know how to do this without a extension method, I am thinking yield may have something to do with it, but don't understand it really well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改 _List 只是将字段(而不是实际列表)更改为完全不相关的列表。简而言之,继承并不能很好地满足您的需求;你不能重新分配“this”(至少不能重新分配一个类)。
我怀疑封装该列表会更适合您。
Changing _List is simply changing the field (not the actual list) to a completely unrelated list. In short inheritance isn't going to work well for what you want here; you can't reassign "this" (at least, not for a class).
I suspect encapsulating the list would work better for you.
您对
ToList
的调用会创建一个不再等于this
的新引用。看起来你可能会更好地通过保留这个列表来让这个类成为一个“帮助者”:
Your call to
ToList
creates a new reference which is no longer equal tothis
.It looks like you probably would be better off with this class being a "helper" by holding onto the list:
根据我得到的建议,问题似乎是 Linq 查询中的 .ToList 引用了一个新列表。所以我只是在 Linq 查询之后添加了 Clear 和 AddRange。但最好的方法是不要尝试继承。所以这就是答案。
From the suggestion I got the problems seems to be that the .ToList in the Linq query reference a new list. So I just have added a Clear and a AddRange after the Linq query. But the best way would have been to not try inheritance. So here is the answer.