LINQ using a where clause to loop through a different array in c#

Viewed 456

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
3 Answers

You can use Contains method. Try like:

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

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));

Try this:

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