我有三個長度相等的不同List<string>
,它們包含不同類型的數據。例如:使用lambda函數合併包含不同對象的列表
List<string> dates = new List<string>() { "20120301", "20120401", "20120501", "20120601", "20120701"};
List<string> times = new List<string>() { "0500", "0800", "0100", "1800", "2100" };
List<string> quantities = new List<string>() { "1", "2", "1", "3", "1" };
實際數據可能是任何東西,但列表總是具有相同的長度。我想將它們合併成一個List<DTQ>
。
public struct DTQ
{
DateTime dt;
double q;
public DTQ(DateTime dt, double q) { this.dt = dt; this.q = q; }
}
有沒有辦法用lambda函數做到這一點?到目前爲止,我已經成功地創建一個描述我會怎樣,如果它是三個strings
代替List<string>
S中的數據映射lambda函數:
Func<string, string, string, DTQ> mergeFields = (d, t, q)
=> new DTQ(DateTime.ParseExact(string.Format("{0}{1}", d, t), "yyyyMMddhhmm", CultureInfo.InvariantCulture), double.Parse(q));
我不知道在那裏我可以從那裏,雖然。這個想法是將這個函數應用到列表的每個索引。
你想怎麼映射呢? – Romoku 2013-04-09 17:45:12
第一項應該是(2012年3月1日05:00,1)。第二項應該是(2012年4月1日08:00,2)。等等,爲五個條目。 – Otaia 2013-04-09 17:49:42