我有3個陣列與下面的數據類型:重新組織出值基於值的唯一組合的陣列的2 C#
string[] arrID = {"111", "222", "333", "444", "555", "666", "777"};
DateTime[] arrDates = new DateTime[]
{
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 21),
new DateTime(2015, 03, 21),
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 20)
};
string[] arrTime = {"8:20", "8:40", "8:20", "9:10", "8:20", "9:10", "8:20"};
我已經加入樣品的元素到3個陣列,以模擬我們將在這些數組中使用的數據類型。
每個這些元素中的每個索引號都包含與一條記錄相關的值。
示例:每個索引爲3的數組中的值都包含構成一條記錄的值。
我需要把這些3個數組值代入下3個數組:arrNEWID
,arrNEWDate
和arrNEWTime
基於下列條件:
- 對於每個唯一的日期和時間的組合,單獨的元件需要是添加到每個陣列。
- 如果一個唯一的日期和時間組合具有多個ID,那麼這些ID需要用逗號分隔。
預期輸出
--------------------------------------------------
| index | arrNEWDate | arrNEWTime | arrNEWID |
--------------------------------------------------
| 0 | 03/20/2015 | 8:20 | 111,333,777 |
| 1 | 03/20/2015 | 8:40 | 222 |
| 2 | 03/20/2015 | 9:10 | 666 |
| 3 | 03/21/2015 | 8:20 | 555 |
| 4 | 03/21/2015 | 9:10 | 444 |
注:
- 在3點新的陣列的數據類型需要是串
- 輸入和輸出需要保持爲數組。列表裏可以用於處理
這裏創建是我已經使用的代碼:
string[] arrID = { "111", "222", "333", "444", "555", "666", "777" };
DateTime[] arrDates = new DateTime[]
{
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 21),
new DateTime(2015, 03, 21),
new DateTime(2015, 03, 20),
new DateTime(2015, 03, 20)
};
string[] arrTime = { "8:20", "8:40", "8:20", "9:10", "8:20", "9:10", "8:20" };
string str_arrNEWID = "";
string str_arrNEWDate = "";
string str_arrNEWTime = "";
string[] arrNEWID = "".Split('~');
string[] arrNEWDate = "".Split('~');
string[] arrNEWTime = "".Split('~');
for (int i = 0; i <= arrID.GetUpperBound(0); i++)
{
int intExists = 0;
for (int j = 0; j <= arrNEWDate.GetUpperBound(0); j++)
{
//check if date matches for the current index being checked
if (arrNEWDate[j].ToString() == arrDates[i].Date.ToString("MM/dd/yyyy"))
{
//check if time matches for the same index
if (arrNEWTime[j].ToString() == arrTime[i].ToString())
{
//existing record
intExists = 1;
arrNEWID[j] = arrNEWID[j] + "," + arrID[i];
str_arrNEWID = string.Join("~", arrNEWID);
}
}
}
if (intExists == 0)
{
//new record
str_arrNEWDate = str_arrNEWDate + "~" + arrDates[i].Date.ToString("MM/dd/yyyy") ;
arrNEWDate = str_arrNEWDate.Split('~');
str_arrNEWTime = str_arrNEWTime + "~" + arrTime[i].ToString() ;
arrNEWTime = str_arrNEWTime.Split('~');
str_arrNEWID = str_arrNEWID + "~" + arrID[i];
arrNEWID = str_arrNEWID.Split('~');
}
}
的主要挑戰是,需要使用編譯這段代碼的編譯器不支持拉姆達運算符(=>)
我需要知道:
如果有辦法做到這一點,這樣我就不必使用臨時字符串變量和分割功能,每次
刷新陣列如果有任何的方式來擺脫多重循環的
你有什麼試過?該代碼做了什麼?這與你期望或想要做的有什麼不同?請參閱http://stackoverflow.com/help/how-to-ask獲取有關如何以清晰,可回答的方式表達您的問題的建議。 – 2015-03-24 21:22:50
我已添加這些詳細信息。感謝您的時間:) – slayernoah 2015-03-25 03:11:14
問題已經更新。請刪除或解釋投票 – slayernoah 2015-03-25 03:14:14