合并c#中的2个复杂列表(merge 2 complex lists in c#)
我在silverlight编程(c#.net)
假设我有一个“数据”类型的列表
public class data { public string QUOTE_ID { get; set; } public string EVENT_ACTION_CD { get; set; } public string CUSTOMER_NAME { get; set; } public string ADAPTIV_CODE { get; set; } }问题是一些数据来自1个数据库而其他数据来自另一个数据库,所以现在我分两步得到数据 - 所以我有这样的东西(使用随机数):
input1 = new List<data> //data return from database 1 //(the data is actually returned as a datable which i convert to a list //to put to a datagrid, but the end result is shown below) { new data { QUOTE_ID = "1", EVENT_ACTION_CD = "2"}, new Project { QUOTE_ID = "2", EVENT_ACTION_CD = "4"}, new Project { QUOTE_ID = "3", EVENT_ACTION_CD = "5"} }; input2 = new List<data> //data return from database 2 { new data { QUOTE_ID = "1", CUSTOMER_NAME = "2", ADAPTIV_CODE ="5"}, new Project { QUOTE_ID = "2", CUSTOMER_NAME = "4", ADAPTIV_CODE = "5"}, new Project { QUOTE_ID = "3", CUSTOMER_NAME = "5", ADAPTIV_CODE = "7"} };所以我应该有像input1这样的2个列表:
(1, 2, null, null 2, 4, null, null 3, 5, null, null)和input2:
(1, null, 2, 5 2, null, 4, 5 3. null, 5, 7)我如何将它们连接在一起形成一个输入列表
(1, 2, 2, 5 2, 4, 4, 5 3, 5, 5, 7)I am programming in silverlight (c# .net)
lets say I have a list of type "data"
public class data { public string QUOTE_ID { get; set; } public string EVENT_ACTION_CD { get; set; } public string CUSTOMER_NAME { get; set; } public string ADAPTIV_CODE { get; set; } }the problem is some of the data comes from 1 database and the other data comes from another, so right now i get the data in 2 steps - so i have something like this (using random numbers):
input1 = new List<data> //data return from database 1 //(the data is actually returned as a datable which i convert to a list //to put to a datagrid, but the end result is shown below) { new data { QUOTE_ID = "1", EVENT_ACTION_CD = "2"}, new Project { QUOTE_ID = "2", EVENT_ACTION_CD = "4"}, new Project { QUOTE_ID = "3", EVENT_ACTION_CD = "5"} }; input2 = new List<data> //data return from database 2 { new data { QUOTE_ID = "1", CUSTOMER_NAME = "2", ADAPTIV_CODE ="5"}, new Project { QUOTE_ID = "2", CUSTOMER_NAME = "4", ADAPTIV_CODE = "5"}, new Project { QUOTE_ID = "3", CUSTOMER_NAME = "5", ADAPTIV_CODE = "7"} };so i should have 2 lists like input1:
(1, 2, null, null 2, 4, null, null 3, 5, null, null)and input2:
(1, null, 2, 5 2, null, 4, 5 3. null, 5, 7)how do i join them together to form one input list to become
(1, 2, 2, 5 2, 4, 4, 5 3, 5, 5, 7)最满意答案
将linq与join运算符一起使用。
请参阅http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
var resultList = (from item in input1 join item2 in input2 on item2.QUOTE_ID equals input2.QUOTE_ID let item.CUSTOMER_NAME = item2.CUSTOMER_NAME let item.ADAPTIV_CODE = item2.ADAPTIV_CODE select item).ToList();Use linq with a join operator.
See http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
var resultList = (from item in input1 join item2 in input2 on item2.QUOTE_ID equals input2.QUOTE_ID let item.CUSTOMER_NAME = item2.CUSTOMER_NAME let item.ADAPTIV_CODE = item2.ADAPTIV_CODE select item).ToList();更多推荐
发布评论