Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以访问在单独类中创建的列表的内容?C_C#_List_Linq To Entities - Fatal编程技术网

C# 是否可以访问在单独类中创建的列表的内容?C

C# 是否可以访问在单独类中创建的列表的内容?C,c#,list,linq-to-entities,C#,List,Linq To Entities,问:我如何使用希望读取列表中每一行的政治分支、电话号码、地址的方法访问单独类中的列表人员?我使用术语行是因为列表是从db表的linq to entities查询填充的 以下是一个例子: public partial class Person //class comes from entity framework. Data table Person which has several fields (i.e. fname, lname, politicalAffiliation, phoneN

问:我如何使用希望读取列表中每一行的政治分支、电话号码、地址的方法访问单独类中的列表人员?我使用术语行是因为列表是从db表的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);

您需要将列表作为属性公开。不可能,也不可能太简单。谢谢-这确实让我有机会访问列表。太好了!请将此标记为问题的解决方案,以便站点可以查看已解决的帖子。Jeffrey Wieder-您可以帮助迭代列表吗…?您只需要循环浏览。Jeffrey Wieder中的foreach人p-我熟悉foreach循环。实际问题比我在问题中提出的一般数据更难解释。我需要测试列表中每一行的值,当一行起作用时,停止遍历列表。我试图使用表中的数据连接到特定设备