1

I have two lists like below.

List<string> apple= new List<string>();
  apple.Add("a");
  apple.Add("b");

List<string> ball= new List<string>();
  ball.Add("a,b");
  ball.Add("b,c");
  ball.Add("c,a");

Now I want to remove elements from the ball list that of which there are substrings of those elements in the apple list. That is if "a" occurs in ball list element, I have to remove those listelements from the ball list.

My expected output of ball list should be (b,c).

2 Answers 2

2

Here a quick LinqPad

var apple= new List<string>();
apple.Add("a");

var ball= new List<string>();
ball.Add("a,b");
ball.Add("b,c");
ball.Add("c,a");

ball.Where(b=>apple.Any(b.Contains))
    .ToList() //create a duplicate, also lists have ForEach
    .ForEach(b=>ball.Remove(b));

ball.Dump();

Output is:

b,c
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

ball = ball.Where(b => !apple.Any(a => b.Contains(a))).ToList();

1 Comment

This will cause a compile time error. "Where" returns an IEnumerable but ball is a list. ToList is needed after Where. Also the request was to remove items from the list, not replace the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.