List<PaymentType> paymentOptions = _PaymentMethods;
_PaymentMethods
是公共财产,我想过滤它的副本并将其存储在变量中paymentOptions
List<Int32> noInvoice = new List<Int32>(){ 16, 4 , 6 };
foreach (PaymentType pt in paymentOptions)
{
if(noInvoice.Contains(pt.Id))
{
paymentOptions.Remove(pt);
break;
}
}
但是,如果您第二次运行,该变量_PaymentMethods
将不再包含已删除的项目。
它似乎是按引用而不是按值...我不想将列表复制到数组我应该使用 Linq 还是有其他方法?
编辑:我现在有这个:
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;
}