我正在使用List,其中AccountInformation有三个属性
public class AccountInformation
{
public string AccountNumber{ get; set; }
public int StartDate { get; set; }
public int EndDate{ get; set; }
}
现在我的数据列表就像这样
AccountNumber StartDate EndDate
AC1 20150101 20150110
AC1 20150110 20150111
AC1 20150111 20150112
AC2 20150112 20150115
AC1 20150116 20150120
AC1 20150121 20150125
AC2 20150125 20150130
AC2 20150130 20150205
现在我需要以下面的方式将这些数据作为最终输出
AccountNumber StartDate EndDate
AC1 20150101 20150111
AC2 20150112 20150115
AC1 20150116 20150120
AC1 20150121 20150125
AC2 20150125 20150005
意味着无论我将连续的AccountNumber视为相同而第一行的EndDate与下一行的StartDate相同,我需要合并这些行。
感谢任何帮助。
答案 0 :(得分:2)
class Program
{
public class AccountInformation
{
public string AccountNumber { get; set; }
public int StartDate { get; set; }
public int EndDate { get; set; }
}
static void Main(string[] args)
{
List<AccountInformation> accounts = new List<AccountInformation>();
accounts.Add(new AccountInformation() { AccountNumber = "AC1", StartDate = 20150101, EndDate = 20150110 });
accounts.Add(new AccountInformation() { AccountNumber = "AC1", StartDate = 20150110, EndDate = 20150111 });
accounts.Add(new AccountInformation() { AccountNumber = "AC1", StartDate = 20150111, EndDate = 20150112 });
accounts.Add(new AccountInformation() { AccountNumber = "AC2", StartDate = 20150112, EndDate = 20150115 });
accounts.Add(new AccountInformation() { AccountNumber = "AC1", StartDate = 20150116, EndDate = 20150120 });
accounts.Add(new AccountInformation() { AccountNumber = "AC1", StartDate = 20150121, EndDate = 20150125 });
accounts.Add(new AccountInformation() { AccountNumber = "AC2", StartDate = 20150125, EndDate = 20150130 });
accounts.Add(new AccountInformation() { AccountNumber = "AC2", StartDate = 20150130, EndDate = 20150205 });
List<AccountInformation> newAccounts = new List<AccountInformation>();
AccountInformation previousAccount = null;
bool continous = false;
for (int index=0;index<accounts.Count;index++)
{
if (null != previousAccount)
{
if (accounts[index].AccountNumber.Equals(previousAccount.AccountNumber) &&
accounts[index].StartDate == previousAccount.EndDate)
{
continous = true;
if (!(continous && newAccounts.Count>0))
{
newAccounts.Add(previousAccount);
}
}
else
{
continous = false;
newAccounts.Add(accounts[index]);
}
}
previousAccount = accounts[index];
}
}
}
答案 1 :(得分:0)
int outer = 0;
while (outer < accInfo.Count - 1)
{
if (accInfo[outer].AccountNumber == accInfo[outer + 1].AccountNumber && accInfo[outer].EndDate == accInfo[outer + 1].StartDate)
{
if (resultAccInfo.Count == 0)
{
resultAccInfo.Add(new AccountInformation()
{
AccountNumber = accInfo[outer].AccountNumber,
StartDate = accInfo[outer].StartDate,
EndDate = accInfo[outer + 1].EndDate
});
}
else
{
resultAccInfo[resultAccInfo.Count - 1].EndDate = accInfo[outer + 1].EndDate;
}
outer++;
}
else
{
resultAccInfo.Add(new AccountInformation()
{
AccountNumber = accInfo[outer + 1].AccountNumber,
StartDate = accInfo[outer + 1].StartDate,
EndDate = accInfo[outer + 1].EndDate
});
outer++;
}
}