我是 .Net 的新手。我有两个对象,Customer
和 Country
在我的项目中:
public class Customer
{
public int CustomerId {get;set;}
public string CustomerName {get;set}
public String Street {get;set}
public String Address {get;set}
public int CountryId {get;set}
//...o
}
public class Country
{
public int CountryId {get;set;}
public String CountryName{get;set}
}
我有两个列表,List<Customer>
和 List<Country>.
我正在使用 sql join 语句来列出具有国家/地区的客户。但是我很困惑如何创建一个包含客户和国家/地区名称的列表。
是否有必要为此创建单独的类?
提前致谢。
最佳答案
不要将国家/地区 ID 存储在您的 Customer 类中,只需存储对象本身。也就是说,让它像这样:
public Class Customer
{
public int CustomerId {get;set;}
public string CustomerName {get;set}
public String Street {get;set}
public String Address {get;set}
public Country Country {get;set}
//...o
}
然后,您的 Customer
对象中就拥有了所有需要的信息,而无需尝试合并列表或类似的事情。
在您的 SQL
语句中,加入国家/地区表并获取国家/地区名称作为结果集的一部分,然后您可以在一次服务器往返中填充客户列表。
关于c# - 通过合并两个列表创建一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11016318/