0

I have a list of strings like this:

    List<string> excelList = new List<string>();
    excelList.Add("ZArA");  excelList.Add("CalviN"); excelList.Add("BaD ZAra");

And I create a Hashset from it like this:

var hashet = new HashSet<string>(excelList,StringComparer.OrdinalIgnoreCase);

And then I have a list of objects of a class like this:

public class MyDbObjects
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int ID { get; set; }
}

List<MyDbObjects> dbObjectses = new List<MyDbObjects>();

// call .add here to add some actual objects to it.

Ok now I want to make sure that ALL the string values in the excelList are existent in my class objects list of dbObjectses as a FirstName or LastName and DON'T CARE ABOUT CASE SENSITIVITY . My code below works BUT it does NOT handle case sensitivity. How do I add that to it?

    var allofThemExist = dbObjectses.All(x => hashet.Contains(x.FirstName) || hashet.Contains(x.LastName));
Bohn
  • 26,091
  • 61
  • 167
  • 254
  • I think your code doesn't match the text? You said "all string values in excelList exist in your class objects", but you check that all the values in your class objects exist in excelList – canton7 Apr 03 '19 at 14:10
  • Did you see [this](https://stackoverflow.com/questions/16922435/make-hashsetstring-case-insensitive)? – mm8 Apr 03 '19 at 14:14
  • Maybe you can use `.Any(y=> string.Equals(y, x.FirstName, StringComparer.OrdinalIgnoreCase))` instead of `.Contains` and the same for `LastName` – IMujagic Apr 03 '19 at 14:15
  • Why do you think your code doesn't handle case sensitivity? The `HashSet`'s comparer is case insensitive, so its `.Contains` method will be case insensitive. – ProgrammingLlama Apr 03 '19 at 14:15
  • 3
    @Andy I don't think this is a duplicate of the linked question. He already has a `HashSet` which uses `StringComparer.OrdinalIgnorecase`. – canton7 Apr 03 '19 at 14:15
  • @canton7 yep, I applied what I could from those other links ... but now stuck in the case sensitive part... not a duplicate. – Bohn Apr 03 '19 at 14:17
  • @Bohn I'm with John - I don't think the problem is where you think it is. If you post a [mcve], we can investigate for ourselves. – canton7 Apr 03 '19 at 14:18
  • By the way [the case insensitivity works great](https://rextester.com/JYU69930). – ProgrammingLlama Apr 03 '19 at 14:20

2 Answers2

2

This should do it:

var hasAll = !excelList.Except(
                dbObjectses.Select(x => x.FirstName).Concat(
                dbObjectses.Select(x => x.LastName)), 
              StringComparer.OrdinalIgnoreCase).Any();

Except uses hashtables internally so should have good performance.

Magnus
  • 45,362
  • 8
  • 80
  • 118
1

instrad of hasset, use the excelList,

var allofThemExist = dbObjectses.All(x => excelList.Contains(x.FirstName) || excelList.Contains(x.LastName));
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • nop, we want the REVERSE logic. Making sure whatever is in excel list, also exists in dbOjects list – Bohn Apr 03 '19 at 14:29
  • @Bohn it's almost like your faulty logic was the problem, and not the case sensitivity of the HashSet. Then again you said you ran the code and that's how you know the HashSet the problem, so I assume Uzair's logic is fine since you've decided it's the HashSet. – ProgrammingLlama Apr 03 '19 at 14:31