Returning the innerlist from a custom collection inheriting from List<T>
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.
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.
Your call to ToList
creates a new reference which is no longer equal to this
.
It looks like you probably would be better off with this class being a "helper" by holding onto the list:
public class OrderDetailCollection
{
private List<OrdersDetail> _List;
public OrdersDetailCollection(IEnumerable<OrdersDetail> input)
{
this._List = input.OrderBy(xx => xx.Item)
.ThenBy(xx => xx.DateExp)
.ToList();
RemoveDuplicates();
}
public IEnumerable<OrdersDetail> CalculateQuantity()
{
return from xx in this._List
where double.Parse(xx.QteBO) > 0.0
orderby xx.DateExp, xx.Item
select xx;
}
}
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.
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();
this.Clear();
this.AddRange(_List);
}
public void CalculateQty()
{
RemoveDuplicate();
_List = (from l in _List
where double.Parse(l.QteBO) > 0
orderby l.DateExp, l.Item
select l).ToList();
this.Clear();
this.AddRange(_List);
}
精彩评论