locked
Error Cannot implicitly convert type System.Collections.Generic.List to 'System.Collections.Generic.IEnumerable RRS feed

  • Question

  • User342385972 posted

    I get an error in the return value - > Error Cannot implicitly convert type System.Collections.Generic.List to 'System.Collections.Generic.IEnumerable

    Please help

    public class Users
    {
    public string Email { get; set; }
    public string UserName { get; set; }
    public string DisplayName { get; set; }
    public bool isMapped { get; set; }
    }

    public IEnumerable<string> Get()
    {
    List<Users> lstADUsers = new List<Users>();
    string DomainPath = "LDAP://DC=teksite,DC=local";
    DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
    DirectorySearcher search = new DirectorySearcher(searchRoot);
    search.Filter = "(&(objectClass=user)(objectCategory=person))";
    search.PropertiesToLoad.Add("samaccountname");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("usergroup");
    search.PropertiesToLoad.Add("displayname");//first name
    SearchResult result;
    SearchResultCollection resultCol = search.FindAll();
    if (resultCol != null)
    {
    for (int counter = 0; counter < resultCol.Count; counter++)
    {
    string UserNameEmailString = string.Empty;
    result = resultCol[counter];
    if (result.Properties.Contains("samaccountname") &&
    result.Properties.Contains("mail") &&
    result.Properties.Contains("displayname"))
    {
    Users objSurveyUsers = new Users();
    objSurveyUsers.Email = (String)result.Properties["mail"][0] +
    "^" + (String)result.Properties["displayname"][0];
    objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
    objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
    lstADUsers.Add(objSurveyUsers);
    }
    }

    string lines = string.Join(Environment.NewLine, lstADUsers);
    return lstADUsers; // here I get error
    }

    return lstADUsers;  // here I get error
    }

    Monday, September 2, 2019 2:02 AM

Answers

  • User283571144 posted

    Hi asif62000,

    According to your codes, I found your Get method return value's type is IEnumerable<string> , but the lstADUsers's type is List<Users>. We couldn't directly convert the List<Users> to  IEnumerable<string>.

    If you want to return a list of the Users, I suggest you could try to use List<Users> or IEnumerable<Users> as the result type. 

    More details, you could refer to below codes:

    public List<Users> Get()
    {
    List<Users> lstADUsers = new List<Users>();
    string DomainPath = "LDAP://DC=teksite,DC=local";
    DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
    DirectorySearcher search = new DirectorySearcher(searchRoot);
    search.Filter = "(&(objectClass=user)(objectCategory=person))";
    search.PropertiesToLoad.Add("samaccountname");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("usergroup");
    search.PropertiesToLoad.Add("displayname");//first name
    SearchResult result;
    SearchResultCollection resultCol = search.FindAll();
    if (resultCol != null)
    {
    for (int counter = 0; counter < resultCol.Count; counter++)
    {
    string UserNameEmailString = string.Empty;
    result = resultCol[counter];
    if (result.Properties.Contains("samaccountname") &&
    result.Properties.Contains("mail") &&
    result.Properties.Contains("displayname"))
    {
    Users objSurveyUsers = new Users();
    objSurveyUsers.Email = (String)result.Properties["mail"][0] +
    "^" + (String)result.Properties["displayname"][0];
    objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
    objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
    lstADUsers.Add(objSurveyUsers);
    }
    }
    
    string lines = string.Join(Environment.NewLine, lstADUsers);
    return lstADUsers; // here I get error
    }
    
    return lstADUsers;  // here I get error
    }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 2, 2019 5:58 AM