如何合并两个列表(列表). [英] How to Merge Two List (lists).
问题描述
大家好,
我正在构建一个应用程序,在其中我必须存储运输商的数据以及与我们讨价还价时他们提供的报价.我有两个对象类型列表.
列出<报价> lstQuotations = quotationBL.getQuotations(quotation);
列出< Transporter> transporterDetails = transBL.getTransporterList(lstQuotations);
getQuotations和getTransporterList是返回这些列表的函数.
quotationBL和transBL是将执行带到业务逻辑类的对象.
现在说到重点,我想将上述两个列表(lstQuotations和transporterDetails)合并为一个结构(例如DataTable或另一个List< someothertype>等).
伙计们告诉我如何以编程方式解决此问题,解决此问题后,我将通过DataBind()将新结构绑定到我的Repeater Control.
对不起,我的英语不好,或者这个问题听起来很难听.
Hello Guys,
I am building an application in which i have to store the data of transporter and the quotation they give when we bargain with them. I have two lists of object type.
List <Quotations> lstQuotations = quotationBL.getQuotations(quotation);
List <Transporter> transporterDetails = transBL.getTransporterList(lstQuotations);
getQuotations and getTransporterList are the functions that returns these lists.
quotationBL and transBL are the objects that take the execution to business logic class.
Now Coming to the point, i want to Merge the above mentioned two lists (lstQuotations and transporterDetails ) into a single structure (like DataTable or another List<someothertype>, etc).
Guys tell me how to solve this problem programmatically, after solving this problem i will Bind the new structure to my Repeater Control via DataBind().
Sorry about my English or if that question sounds ugly.
推荐答案
我没有比这更优雅的解决方案了:
- 创建一个包含两列的数据表(我假设您知道如何执行此操作);或
- 创建一个包含两个项目的数据类,并列出它们.
I''m not aware of a more elegant solution than either:
- Create a data table with two columns (I assume you know how to do this); or
- Make a data class that contains the two items and make a list of those.
class QuotationTransporter {
public Quotation Quotation { get; set; }
public Transporter Transporter { get; set; }
public QuotationTransporter(Quotation q, Transporter t){
Quotation = q; Transporter = t;
}
}
List<QuotationTransporter> Merge(List<Quotation> q, <Transporter> t){
if(q.Count != t.Count) throw new ArgumentException("Lists must be same length");
List<QuotationTransporter> r = new List<QuotationTransporter>(q.Count);
for(int i = 0; i < q.Count; i++)
r.Add(new QuotationTransporter(q[i], t[i]);
return r;
}
这篇关于如何合并两个列表(列表).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!