我是.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连接语句来列出国家的客户。 但我很困惑,如何创建一个包含客户和国名的单子列表。
是否有必要为此创建单独的类?
提前致谢。
I am new to .Net. I have two objects, Customer and Country in my project :
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} }I have two lists, List<Customer> and List<Country>.
I am using a sql join statement to list customers with country. But I am confused how can I create a single list which contains customers along with the country name.
Is it necessary to create separate class for that?
Thanks in advance.
最满意答案
而不是将Country 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语句中,加入国家/地区表并将国家/地区名称作为结果集的一部分,然后您可以在一次服务器往返中填充客户列表。
Instead of storing country id in your Customer class, just store the object itself. That is, make it like this:
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 }Then you have all the needed info in your Customer object, and there's no need to try to merge lists or anything like that.
On your SQL statement, join to country table and get the country name as part of the result set, then you can populate the customer list in one server round trip.
更多推荐
发布评论