c# - 通过合并两个列表创建一个列表

标签 c#

我是 .Net 的新手。我有两个对象,CustomerCountry在我的项目中:

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/

相关文章:

c# - WebBrowser 滚动到 tr

c# - 在父方法中访问子对象的静态属性

c# - IIS Express 路由在 Visual Studio 2019 中无法正常工作

c# - 使用 linq 从动态 sql 查询中检索和打印数据

c# - DOC 到 PDF 库(不一定免费)

c# - 我怎样才能正确测试我传递给 Controller ​​的模型是否属于特定类型?

c# - 将模型集合中的一项发布到 MVC 中的 Controller 方法

c# - 使用 foo[bar] = baz 添加到 ConcurrentDictionary 是线程安全的吗?

c# - 简单的正则表达式在 c# 中不起作用

c# - 阻止代码并等待事件处理程序触发?