2

I would like to return a list of names that do not exist in report. However I am not sure how to loop properly through IEnumerable<string> names using a LINQ. Is there a way to loop through another array using LINQ ?

    private class Report
        {
            public string UserName { get; set; }
            public string city { get; set; }
            public string image { get; set; }
        }


  List<Report>report = await _service(id).ConfigureAwait(false);
  IEnumerable<string> names = await _names(id).ConfigureAwait(false);

// only want to get list of names that do not exist in report

 var newList = reports.Where(x => x.UserName.Where(i => != names)); // doesn't work
1
  • 1
    I would struct names as a Hashset or Dictionary to prevent O(n^2) Commented Oct 23, 2020 at 16:37

3 Answers 3

3

You can use Contains method. Try like:

var newList = reports.Where(x => !names.Contains(x.UserName)));
Sign up to request clarification or add additional context in comments.

Comments

1

only want to get list of names that do not exist in report

You want to query on names then, to get names back that don't exist.

var newList = names.Where(x => !reports.Any(xx => xx.UserName == x));

1 Comment

You may also want to add a string.equals(xx, xx.UserName, StringComparison.OrdinalIgnoreCase) check instead of a straight equals
0

Try this:

 var newList = names.Where(n => reports.Any(r => r.UserName != n));

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.