Hy,我有点卡住了。我试图在其他课程中到达List的字段。这是我的代码示例:
装载程序类:
public List<Contact> LoadLicenses()
{
var listOfClients = new List<Contact>();
using (var connection = new SqlConnection())
{
connection.ConnectionString =
ConfigurationManager.ConnectionStrings["ContactManagerContext"].ConnectionString.ToString();
connection.Open();
string sql = "SELECT * FROM [dbo].[Contacts]";
using (var command = new SqlCommand(sql, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var licenses = new Contact();
licenses.ContactId = Convert.ToInt32(reader["ContactId"]);
licenses.Name = reader["Name"].ToString();
licenses.Address = Convert.ToInt32(reader["Address"]);
licenses.Email = reader["Email"].ToString();
listOfClients.Add(licenses);
}
}
}
}
return listOfClients;
}
ClientDuration类:
public List<Contact> DurationLeft(/*DateTime _date*/)
{
Loader loader = new Loader();
List<Contact> clientContacts = new List<Contact>();
return clientContacts;
}
在ClientDuration
课程中,我想做一些事情,比如从列表中获取字段(电子邮件,地址,...),并在Loader类中声明。我在LoadLicenses
方法中调用DurationLeft
,然后将其分配给与在Loader类中相同类型的新列表。但后来我无法到达田野。
怎么做?请帮忙!
答案 0 :(得分:0)
试试这个
public List<Contact> DurationLeft(/*DateTime _date*/)
{
Loader loader = new Loader();
List<Contact> clientContacts = new List<Contact>();
clientContacts =loader.LoadLicenses();
var firstemailfield=clientContacts[0].Email;
return clientContacts;
}
答案 1 :(得分:0)
您有Contacts
的集合,在访问其中的属性/字段之前,您必须挑出一个联系人
即
foreach(Contact contact in clientContacts)
{
Console.WriteLine(contact.name):
}
或者
clientContacts[0].name // access first contact in list