我有一個類:C#如何調用爲每個項目的方法在一個List <>
[DataContract]
public class InventoryItem : NotifyPropertyChangeObject
{
private Guid _inventoryItemUid;
private string _description;
private bool _isActive;
[DataMember]
public Guid InventoryItemUid {
get { return _inventoryItemUid; }
set { ApplyPropertyChange<InventoryItem, Guid>(ref _inventoryItemUid, o => o.InventoryItemUid, value); }
}
[DataMember]
public string Description {
get { return _description; }
set { ApplyPropertyChange<InventoryItem, String>(ref _description, o => o.Description, value); }
}
[DataMember]
public bool IsActive {
get { return _isActive; }
set { ApplyPropertyChange<InventoryItem, Boolean>(ref _isActive, o => o.IsActive, value); }
}
public void CustomMethod()
{
// here some code....
}
}
在我的應用程序代碼中,我得到一個List<InventoryItem>
:
List<Entities.InventoryItem> items = new List<Entities.InventoryItem>();
var newItem = new Entities.InventoryItem();
using (Logistics.Data.Warehouse svc = new Data.Warehouse())
{
items = svc.GetInventoryItems().ToList();
}
我需要調用每一個項目List<InventoryItem>
方法CustomMethod()
。
在性能方面做什麼最好的方法是什麼?
我知道我可以做一個foreach
,但如果我得到5000行,那麼也許foreach
是不好的表現。其實這CustomMethod()
就像一個初始化代碼的東西。
有沒有辦法繞過它,你需要一個循環 –
但是,如果你仍然需要調用每一行的方法,如果你有5行或5000是如何重要?也許你可以解釋這個方法是什麼(除非它真的對每一行都做了些什麼,然後就沒有辦法)。 – master2080
如果需要在每個項目上調用它,爲什麼不把它放在構造函數中呢? – tomassino