是否可以访问在单独类中创建的列表的内容?C#
本文关键字:列表 创建 访问 单独类 是否 | 更新日期: 2023-09-27 18:22:35
问题:如何使用要读取列表中每行的politicalAffiliation、phoneNumber和地址的方法访问单独类中的列表人员?我使用术语"行"是因为列表是从数据库表的linq-to-entities查询填充的。
这里有一个例子:
public partial class Person //class comes from entity framework. Data table Person which has several fields (i.e. fname, lname, politicalAffiliation, phoneNumber, address)
{
public static List<Person> GetInfoForPerson (string fname)
{
List<Person> person;
using (PeopleDBEntities ctx = new PeopleDBEntities())
{
person = (from p in ctx.Person
where p.fName == fname
select p).ToList();
}
return person;
}
}
class ManagePerson
{
private bool GetDefinedInfoForPersonFromFname(person)
{
List<Person> people = Person.GetInfoForPerson(Martha);
if (people.count == 0)
{return false;}
if (people.count !=0)
{return true;}
}
}
class AccessPerson
{
protected void ConnectWithPerson(Person person)
{
// here is where I want to access the list people from the ManagePerson class
//I want to be able to iterate over the list in order to search for certain
//values of fname and lname
// in order to use the values of political affiliation, phoneNumber, and address
//associated with the particular fname and lname match
}
}
只需使其成为所属类的公共成员即可。
您应该执行以下步骤
将此公开
public class ManagePerson
{
和类似的属性
public List<Person> Persons
{get{return people;}
set{ people=value;}}
以及最后一次
List<Person> people; //should be at level class
private bool GetDefinedInfoForPersonFromFname(person)
{
people= Person.GetInfoForPerson(Martha);