0

I have 2 string arrays/values x,y and am trying to get values of y which are not in x. I am trying to get this value only in case if all values of x are also in y.

        string x = "CA ,WA";
        string y = "CA,WA,NY";
        var srcDetails = x.ToLower().Replace(" ", string.Empty).Split(',');
        var dstDetails = y.ToLower().Replace(" ", string.Empty).Split(',');

        var common = dstDetails.Intersect(srcDetails); //common in x,y

        var destGreaterSrc= dstDetails.Except(srcDetails); //if y > x

        var extraInDest = dstDetails.Except(common); 

extraInDest is extra value in y which is not in x

In above code extra values in dest which is outputted as NY.

I am trying to find the scenario where values of x may not be equal to y like

    string x = "CA ,NV";
    string y = "CA,WA,NY";

how can we make var extraInDest output to false.

like var extraInDest = dstDetails.Except(common) resulting false or null

4
  • 1
    Will your string be always in CSV? Commented Oct 18, 2016 at 4:49
  • Always comma seperated. Commented Oct 18, 2016 at 4:50
  • 1
    string x = "CA ,WA"; string y = "CA,WA,NY"; var srcDetails = x.Split(','); var dstDetails = y.Split(','); var extraInSrc = srcDetails.Where(s => !dstDetails.Select(d => d.Trim().ToLower()).Contains(s.Trim().ToLower())).ToList(); if (extraInSrc.Count() <= 0) // If all X values are present in Y { var extraInDst = dstDetails.Where(s => !srcDetails.Select(d => d.Trim().ToLower()).Contains(s.Trim().ToLower())).ToList(); } Commented Oct 18, 2016 at 5:20
  • very interesting code block kashi. Commented Oct 18, 2016 at 5:42

2 Answers 2

1

The simpliest one would be

var extraInDest = srcDetails.Except(dstDetails).Any()
    ? null 
    : dstDetails.Except(srcDetails);
Sign up to request clarification or add additional context in comments.

1 Comment

@Kurkula so does this one helped you?
1

This should be simple, if your comparison should always result in a boolean value, you should use this instead

bool extraInDest = srcDetails.All(v=>dstDetails.Contains(v));

Comments

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.