asp.net - Filter IList<T> -
list<paymenttype> paymentoptions = _paymentmethods; _paymentmethods public property , i'd filter copy of , store in variable paymentoptions
list<int32> noinvoice = new list<int32>(){ 16, 4 , 6 }; foreach (paymenttype pt in paymentoptions) { if(noinvoice.contains(pt.id)) { paymentoptions.remove(pt); break; } } but if run second time, variable _paymentmethods not contain removed item anymore.
it seems go reference instead of value... prefer not copy list array should use linq or there other way?
edit: have now:
list<paymenttype> paymentoptions = shopcontroller.currentshop.paymentmethods; list<paymenttype> paymentoptionsfiltered = new list<paymenttype>(); if (havetofilter) { list<int32> noinvoice = new list<int32>() { 16, 4, 6 }; foreach (paymenttype pt in paymentoptions) { if (!noinvoice.contains(pt.id)) { paymentoptionsfiltered.add(pt); } } repeaterpaymentoptions.datasource = paymentoptionsfiltered; } else { repeaterpaymentoptions.datasource = paymentoptions; }
use linq:
list<paymenttype> paymentoptionswithnoinvoice = paymentoptions.where(a=> !noinvoice.contains(a.id)).tolist();
Comments
Post a Comment