I want to choose a record that is not in the database.

var context = new PrincipalContext(ContextType.Domain); var searcher = new PrincipalSearcher(new UserPrincipal(context)); string objectGUID; foreach (var result in searcher.FindAll()) { DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; Guid _guid = new Guid((byte[])(Array)de.Properties["objectGUID"][0]); objectGUID = _guid.ToString(); var test = db.ADUsers.Where(a => a.ObjectGUID != objectGUID).FirstOrDefault(); } 

I take objectGUID from Active Directory as a kind of unique number and want to compare it with objectGUID in the database and if there is no such objectGUID in the database, then write it there.

I do like this a.ObjectGUID == objectGUID and in the console everything is OK, I get the whole list by coincidence.

  var test = db.ADUsers.Where(a => a.ObjectGUID == objectGUID).FirstOrDefault(); 

But is it logical to do a.ObjectGUID != objectGUID and get data that is not in the table? But this method does not work. Help!

    2 answers 2

    since AD ​​does not support standard SQL queries, I would do this:

    1. Get a list of all objectGUID of interest from AD in the form of a List (for reference, objetGUID is a unique identifier in AD, no other is provided).

    2. We run on the sheet. If the objectGUID in the List found in the database, we delete it from the list.

    3. The remaining in the list is added to the database.

    Well, or immediately add to the database upon detection of the absence of objectGUID in the database.

      Solved the problem in this way:

        var selectListFromDB = db.ADUsers.Select(a => a.ObjectGUID); //Забираем того, что нет в базе var selectDifferenceLists = guidsFromAD.Except(selectListFromDB); var selectUsersList = from ad in searcher.FindAll() from s in selectDifferenceLists where ad.Guid.ToString() == s.ToString() select ad; 

      The link has an Except method that compares two sheets to each other and returns what is not in the first sheet.