我正在尝试从argparse
获取结果,但是我被困住了,不知道该怎么办。
我有DataTable
,由于我的结果存储在public static DataTable VratiKorisnike()
中,因此需要返回DataTable
resultTable
作为结果。
到目前为止,我的功能在这里
resultTable
当我向public static DataTable VratiKorisnike()
{
List<Korisnik> lstADUsers = new List<Korisnik>();
string sDomainName = "saos";
string DomainPath = "LDAP://" + sDomainName;
string output = @"C:\output.txt";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("usergroup");
search.PropertiesToLoad.Add("displayname");
search.PropertiesToLoad.Add("userAccountControl");
search.PropertiesToLoad.Add("PasswordExpirationDate");
//search.PropertiesToLoad.Add("DomainGroup");
DataTable resultsTable = new DataTable();
resultsTable.Columns.Add("samaccountname");
resultsTable.Columns.Add("usergroup");
resultsTable.Columns.Add("displayname");
resultsTable.Columns.Add("userAccountControl");
resultsTable.Columns.Add("PasswordExpirationDate");
//resultsTable.Columns.Add("DomainGroup");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter <= (resultCol.Count - 1); counter++)
{
string UserNameEmailString = string.Empty;
result = search.FindOne();
if ((result.Properties.Contains("samaccountname") && result.Properties.Contains("usergroup")))
{
Korisnik korisnik = new Korisnik();
korisnik.Ime = ((string)(result.Properties["samaccountname"][0]));
korisnik.Prezime = ((string)(result.Properties["displayname"][0]));
korisnik.AccountExpired = (DateTime)result.Properties["userAccountControl"][0];
korisnik.PassNevExp = ((bool)result.Properties["PasswordExpirationDate"][0]);
korisnik.DomenskaGrupa = ((string)(result.Properties["DomainGroup"][0]));
DataRow dr = resultsTable.NewRow();
dr["samaccountname"] = korisnik.Ime;
dr["displayname"] = korisnik.Prezime;
dr["userAccountControl"] = korisnik.AccountExpired;
dr["PasswordExpirationDate"] = korisnik.PassNevExp;
dr["DomainGroup"] = korisnik.DomenskaGrupa;
resultsTable.Rows.Add(dr);
lstADUsers.Add(korisnik);
}
}
var json = JsonConvert.SerializeObject(resultCol, Formatting.Indented);
var res = json;
Console.WriteLine(resultCol);
Console.ReadLine();
File.WriteAllText(output, json);
}
return resultsTable;
}
调用函数时出现错误
Foreach语句无法对'DataTable'类型的变量进行操作 因为“ DataTable”不包含针对的公共实例定义 'GetEnumerator'
Main
我尝试这样的事情:
foreach (Korisnik korisnik in VratiKorisnike())
{
Console.WriteLine(korisnik);
}
错误为foreach(DataTable dt in VratiKorisnike())
{
Console.WriteLine(dt);
}
您知道如何解决此问题吗?
答案 0 :(得分:1)
要将DataTable
用作IEnumerable<DataRow>
,您需要在垂直Linq To DataSet上使用AsEnumerable
foreach (DataRow row in VratiKorisnike().AsEnumerable())
{
string displayName = row.Field<string>("displayname")
...
}
答案 1 :(得分:0)
您需要使用Rows
属性:
foreach (var korisnik in VratiKorisnike().Rows)
它表示一个DataTable的行的集合,和。实际上,它会像这样编译,并且正如您所见,由于GetEnumerator
,您可以在其中使用循环:
IEnumerator enumerator = VratiKorisnike().Rows.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
DataRow value = (DataRow)enumerator.Current;
Console.WriteLine(value);
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}