Benk
Benk

Reputation: 1312

Linq syntax Contains

Linq syntax

Have a tag table: data contains tagName dogs, dog, cat

1 string[] test1 = new[] { "dogs", "dog" };

Compare test1 with tag table and return list of tags not in a test1 result: cat

var item1= Tags.Where(x => !test1.Contains(x.TagName)).ToList();

result "cat" correct

  1. string[] test2 = new[] { "dogs", "dog", "cat", "bird" };

how do I compare test2 with tag table and return new items found in the test2 result should be bird.

var item2= test2.Where(x => !Tags.Contains(test2)).ToList(); XX fails here?

Thanks,

Upvotes: 0

Views: 91

Answers (2)

Rahul Singh
Rahul Singh

Reputation: 21795

Use Except:

            //Datasource
            string[] Tags = { "dogs", "dog", "cat" };
            string[] test2 = new[] { "dogs", "dog", "cat", "bird" };

            var result = test2.Except(Tags);

If you're really interested in doing it with Contains, then you can do like this:-

var result = test2.Where(x => !Tags.Contains(x));

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You can use Any():

var item2= test2.Where(x => Tags.Any(t=> !t.Contains(x))).ToList();

Upvotes: 0

Related Questions