我有两个班:
1. customer
类,包含客户的身份和姓名
2.一个customer_details
类,其中包含客户的地址等等
class customer
{
public int customer_id { get; set; }
public string name { get; set; }
//and so on
}
class customer_details
{
public int customer_id { get; set; }
public string address { get; set; }
//and so on
}
我的问题是,如何将List<customer>
和List<customer_details>
加入单个列表,这样就会向我显示customer_id
,name
和{{1}在一个列表中......
感谢您的回答
答案 0 :(得分:5)
您必须使用加入方法
以下是您的课程
的示例 List<customer> customers = new List<customer>();
List<customer_details> details = new List<customer_details>();
var query = (from c in customers
join d in details on c.customer_id equals d.customer_id
select new
{
c.customer_id,
c.name,
d.address,
}).ToList();
答案 1 :(得分:2)
以下是使用Fluent语法的示例,该语法相当紧凑且首选:
customers.Join(
details,
c=>c.customer_id,
d=>d.customer_id,
(c,d)=>new {c.customer_id, c.name, d.address}
).ToList()