过滤器 IList
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;
}
List<PaymentType> paymentOptions = _PaymentMethods;
_PaymentMethods
is an public property and I'd like to filter a copy of it and store it in the 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 you run this the second time, the variable _PaymentMethods
does not contain the removed item anymore.
It seems to go by Reference instead of by value...
I prefer not to copy the list to an array
Should I use Linq or is there an other way?
EDIT: I have this 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 LINQ:
Use LINQ:
您可以使用
List
复制构造函数 并从副本中删除:You can use the
List<T>
copy constructor and remove from the copy: