我正在使用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"}
};
所以我应该有2个列表 输入1:
(1, 2, null, null
2, 4, null, null
3, 5, null, null)
和 输入2:
(1, null, 2, 5
2, null, 4, 5
3. null, 5, 7)
如何将它们连接在一起形成一个输入列表成为
(1, 2, 2, 5
2, 4, 4, 5
3, 5, 5, 7)
答案 0 :(得分:0)
正常的for循环对你有用:
for(int i = 0; i < input1.Count; i++){
if(input1[i].QUOTE_ID == null) input1[i].QUOTE_ID = input2[i].QUOTE_ID;
if(input1[i].EVENT_ACTION_CD == null) input1[i].EVENT_ACTION_CD = input2[i].EVENT_ACTION_CD;
if(input1[i].CUSTOMER_NAME == null) input1[i].CUSTOMER_NAME = input2[i].CUSTOMER_NAME;
if(input1[i].ADAPTIV_CODE == null) input1[i].ADAPTIV_CODE = input2[i].ADAPTIV_CODE;
}
结果将保存到input1
。该代码还假设input1
和input2
具有相同的Count
。
答案 1 :(得分:0)
var input3 = input1.Join( 输入2 d1 =&gt; d1.QUOTE_ID, d2 =&gt; d2.QUOTE_ID, (d1,d2)=&gt;新数据() { QUOTE_ID = d1.QUOTE_ID, EVENT_ACTION_CD = d1.EVENT_ACTION_CD, CUSTOMER_NAME = d2.CUSTOMER_NAME, ADAPTIV_CODE = d2.ADAPTIV_CODE } );
答案 2 :(得分:0)
将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();