-2

I have and object of type ct_CompleteOrder and have following classes:

ct_CompleteOrder class

public partial class ct_CompleteOrder {
    private ct_CompleteOrderPayments paymentsField;
}

ct_CompleteOrderPayments class

public partial class ct_CompleteOrderPayments {
    private ct_Payment[] paymentField;
    public ct_Payment[] Payment {
    get {
        return this.paymentField;
        }
    set {
        this.paymentField = value;
        }
    }
}

ct_Payment class

public partial class ct_Payment {
    public string type{get; set;}
}

I want to remove elements of the ct_Payment array on the basis of type value. I tried to convert it into list first to apply RemoveAll, but its not working. what am I doing wrong?

completeOrder.Payments.Payment.ToList().RemoveAll(x => x.type == "AUTO");
Huma Ali
  • 1,759
  • 7
  • 40
  • 66
  • A similar SO question is discussed [here](https://stackoverflow.com/questions/853526/using-linq-to-remove-elements-from-a-listt?rq=1) in detail, is this what you are after? – fujiFX Feb 12 '19 at 22:38
  • What you're doing wrong is assuming that LINQ, which is a technology aimed at querying and read only operations, is for manipulating the content of lists/write operations – Caius Jard Feb 12 '19 at 22:39
  • @fujiFX no that is a different query. I'm looking for removing element from array not list – Huma Ali Feb 12 '19 at 22:57
  • @HumaAli Assuming that the query "is different" because you are using an array and not a list when you are using LINQ means to me that you have to read a basic LINQ tutorial. LINQ does not care what your data source is, and this is a duplicate – Camilo Terevinto Feb 12 '19 at 23:29

2 Answers2

2

Why would you want to convert to a list at all? I believe that is an unnecessary step. I have created a DotNetFiddle for you to show you what I did from what I understood about your question.

C#

using System;
using System.Runtime;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string[] arrayOfItems = new string[5] {"Apple", "Banana", "Orange", "Apple", "Grape"};

        var arrayWithoutApples = arrayOfItems.Where(x => x != "Apple").ToArray();

        foreach(var item in arrayWithoutApples)
        {
            Console.WriteLine(item);    
        }

        // Output:

        // Banana
        // Orange
        // Grape

    }
}

My example I'm sure is not as complex as your code, but if you have an array of values and you want to 'slim' that array by removing elements based on a certain condition, then you shouldn't have to convert to a list beforehand. Use Where to retrieve the items that you want or don't want and then use ToArray() to convert the result to an array variable.

Let me know if this helps.

Grizzly
  • 5,873
  • 8
  • 56
  • 109
1

When you copy the array to list and then apply linq, the link is only removing from the list, not from the array.

If you want to keep the array the same size, but with empty spaces, you should walk through the array using a for loop and set any that have x.type == "AUTO" to null.

for(int i = 0; i < completeOrder.Payments.Payment.Length; i++)
{
    if(completeOrder.Payments.Payment[i].type == "AUTO")
    {
        completeOrder.Paymets.Payment[i] == null;
    }
}

Otherwise, if you want the actual size of the array to change just set payment to the altered list. RemoveAll DOES NOT return the list (it returns void), so you might as well reverse your logic and just use a Where statement

completeOrder.Payments.Payment = completeOrder.Payments.Payment.Where(x => x.type != "AUTO").ToArray();
Chaya Sulman
  • 111
  • 4