过滤器 IList

发布于 2024-10-31 17:14:39 字数 1156 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-11-07 17:14:39

使用 LINQ:

List<PaymentType> paymentOptionsWithNoInvoice =  
paymentOptions.Where(a=> !noInvoice.Contains(a.Id)).ToList();

Use LINQ:

List<PaymentType> paymentOptionsWithNoInvoice =  
paymentOptions.Where(a=> !noInvoice.Contains(a.Id)).ToList();
画▽骨i 2024-11-07 17:14:39

您可以使用 List 复制构造函数 并从副本中删除:

var paymentOptionsWithNoInvoice = new List<PaymentType>(_PaymentMethods);
paymentOptionsWithNoInvoice.RemoveAll(pt => noInvoice.Contains(pt.Id));

You can use the List<T> copy constructor and remove from the copy:

var paymentOptionsWithNoInvoice = new List<PaymentType>(_PaymentMethods);
paymentOptionsWithNoInvoice.RemoveAll(pt => noInvoice.Contains(pt.Id));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文