通过组合两个列表来创建一个列表(Creating a List by combining two lists)

编程入门 行业动态 更新时间:2024-10-26 04:23:44
通过组合两个列表来创建一个列表(Creating a List by combining two lists)

我是.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.

更多推荐

本文发布于:2023-04-28 03:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   列表   创建一个   两个   lists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!