我在下面的C#类,并希望根据该人居住的地址创建一个不同的家庭列表。如果他们有相同的地址,他们就是家人。所以它应该有一个家庭名单,每个家庭都有一个家庭成员名单。C#嵌套类列表<>
public class Person
{
public string firstName;
public string lastName;
public Address address;
public Person(string f, string l, string addr, string city, string state)
{
firstName = f;
lastName = l;
address= new Address(addr, city, state);
}
}
public class Family
{
public string familyAddress;//maybe I don't need this
public List<Person> personList;
public Family()
{
personList = new List<Person>();
}
public void AddPerson(Person p)
{
personList.Add(p);
}
public string FamilyAddress //maybe I dont need this
{
get
{
return familyAddress;
}
set
{
familyAddress= value;
}
}
}
public partial class Form1 : Form
{
public List<Family> familyList;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
familyList= new List<Family>();
ReadCustomerFile("..\\Customers.txt");
}
private void ReadCustomerFile(file)
{
var lines = File.ReadAllLines(file);
var reg = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)");
for (int i = 0; i < lines.Length; i++)
{
var data = reg.Matches(lines[i]).Cast<Match>().Select(m => m.Value).ToArray();
//data[0] contains first name
//data[1] contains last name
//data[2] contains street
//data[3] contains city
//data[4] contains state
//In here as I read the customer text file, i would
//like to add the person that has the same address into
//the same family.
//for example:
//familyList address: "800 NE Oregon St. Portland, OR" has
// "Steve Jones"
// "Sarah Jones"
// "Alisa Jones"
//familyList address: "2525 Lake Park. Salt Lake City, UT" has
// "Joey William"
// "Becky William"
// "Sam William"
}
}
}
更新#1-基于Tempx评论:谢谢!
public partial class Form1 : Form
{
public List<Family> familyList;
public List<Person> personList;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
familyList= new List<Family>();
personList = new List<Person>();
ReadCustomerFile("..\\Customers.txt");
}
private void ReadCustomerFile(file)
{
var lines = File.ReadAllLines(file);
var reg = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)");
for (int i = 0; i < lines.Length; i++)
{
var data = reg.Matches(lines[i]).Cast<Match>().Select(m => m.Value).ToArray();
//data[0] contains first name
//data[1] contains last name
//data[2] contains street
//data[3] contains city
//data[4] contains state
Person person = new Person(data[0].Trim(','),
data[1].Trim(','),
data[2].Trim(','),
data[3].Trim(','),
data[4].Trim(','));
personList.Add(person);
}
//In here as I read the customer text file, i would
//like to add the person that has the same address into
//the same family.
//for example:
//familyList address: "800 NE Oregon St. Portland, OR" has
// "Steve Jones"
// "Sarah Jones"
// "Alisa Jones"
//familyList address: "2525 Lake Park. Salt Lake City, UT" has
// "Joey William"
// "Becky William"
// "Sam William"
var familyAddress = new Dictionary<Address, Family>();
Family family;
foreach (Person p in personList)
{
// Returns true if the familyAddess contains the address of the person
if (familyAddress.TryGetValue(p.address, out family))
//if such family exists add the person the family
family.AddPerson(p);
else // no family is found with the person's address
{
//create a new family
family = new Family();
//add person to the family
family.AddPerson(p);
//add the family to the familyAddress dictionary
familyAddress.Add(p.address, family);
}
}
}
}
本声明
if (familyAddress.TryGetValue(p.address, out family))
总是返回家不存在,所以它去else语句
UPDATE#2现在可以加入人的家庭。但是现在有时候人们的地址在逗号和逗号的时间里,尽管街道名称,城市和州是相同的。所以它导致Dictionary键TryGetValue()返回不需要的结果。 TryGetValue()可能会忽略逗号和句点吗?谢谢
public partial class Form1 : Form
{
public List<Family> familyList;
public List<Person> personList;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
familyList= new List<Family>();
personList = new List<Person>();
ReadCustomerFile("..\\Customers.txt");
}
private void ReadCustomerFile(file)
{
var lines = File.ReadAllLines(file);
var reg = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)");
for (int i = 0; i < lines.Length; i++)
{
var data = reg.Matches(lines[i]).Cast<Match>().Select(m => m.Value).ToArray();
Person person = new Person(data[0].Trim(','),
data[1].Trim(','),
data[2].Trim(','),
data[3].Trim(','),
data[4].Trim(','));
personList.Add(person);
}
var familyAddress = new Dictionary<string, Family>();
Family family;
foreach (Person p in personList)
{
// Returns true if the familyAddess contains the address of the person
if (familyAddress.TryGetValue(p.address.GetAddress(), out family))
//if such family exists add the person the family
family.AddPerson(p);
else // no family is found with the person's address
{
//create a new family
family = new Family();
//add person to the family
family.AddPerson(p);
//add the family to the familyAddress dictionary
familyAddress.Add(p.address, family);
}
}
}
}
UPDATE#3-我决定将地址字符串格式化为正确的格式,然后我添加为字典中的键。转换后列出类似下面以下Tempx建议:
var valueList = familyAddress.Values.ToList();
我如何可以遍历目录?因为我尝试以下:
foreach (var h in valueList)
{
foreach(var p in h)
{
//I cannot access the Person data?
}
}
感谢
什么是你的问题? –
“它应该有一个家庭列表,每个家庭都应该有一个家庭成员列表。”它看起来像你的代码已经有这个。 – Vassalware
您似乎未能提出问题。我有一个给你,虽然 - 为什么是“Address”在一个类中表示为一个字符串,而在另一个类中表示为实际类型? –