开发者

Combine two generic lists of different types

I have two lists as

List <BillDetails>= new List<BillD开发者_如何学运维etails>();  
List<Header>=new List<Header>();

Public class Billdetails  
{    
    public string chargecategory;  
    public string materialcode;  
    public int price;  
    public int quantity;  
}

public class Header  
{  
    public string chargecode;  
    public string customername;  
}  

These two lists are not linked in any way. But I have to combine them these two into one list and bind it to a gridview.

Please help me in combining or merging these two lists without anything in common between them.

Thanks in advance.


Assuming they are both in the right order and of the same length (big assumption!) you could zip them up and project to an anonymous type:

    List<BillDetails> billDetails = new List<BillDetails>();
    List<Header> headers =new List<Header>();

    var results = billDetails.Zip(headers, (details, header) => 
                                  new { details.chargecategory, details.materialcode, header.chargecode, header.customername })
                             .ToList();


IEnumerable both = billDetails.OfType<object>().Concat(headers.OfType<object>());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜