[英]Merging Two List with Same Properties into One List
以下是我的实体之一:
[Table("UserPaymentAccountHistoryLog")]
public partial class UserPaymentAccountHistoryLog : IEntity
{
public int UserPaymentAccountHistoryLogID { get; set; }
public Guid UserID { get; set; }
public Guid UserPaymentAccountID { get; set; }
public bool ActionType { get; set; }
public DateTime LogHistory { get; set; }
[Required]
[StringLength(100)]
public string LogDetail { get; set; }
public virtual UserPaymentAccount UserPaymentAccount { get; set; }
}
这是我的另一个实体:
[Table("UserPaymentHistoryLog")]
public partial class UserPaymentHistoryLog : IEntity
{
[Key]
public int UserPaymentHistoryLogID { get; set; }
public Guid UserPaymentAccountID { get; set; }
public Guid UserID { get; set; }
public Guid OrderID { get; set; }
public bool TransactionType { get; set; }
public DateTime LogHistory { get; set; }
[Required]
[StringLength(100)]
public string LogDetail { get; set; }
}
如您所见,它们的某些属性是相同的。 我想做的是,我想将组成这两个实体的两个列表合并到组成下面的实体的一个列表中:
public class CardTransactionHistoryWM
{
public bool ActionType { get; set; }
public bool TransactionType { get; set; }
public DateTime LogHistory { get; set; }
public string LogDetail { get; set; }
public OrderWM Order { get; set; }
}
我遇到了解决方案,建议使用join
和zip
linq查询,但是那些相同的属性使我感到困惑。
重要的部分是,如果UserPaymentAccountHistoryLog
和UserPaymentHistoryLog
具有完全相同的日期记录,则CardTransactionHistoryWM
应该为此日期发布一行,而不是两行,并同时显示UserPaymentAccountHistoryLog
和UserPaymentHistoryLog
LogDetails。
先感谢您!
如果join和/或zip方法过于复杂或难以理解,则另一种方法是将CardTransactionHistoryWM
类扩展为包括:
静态Parse
方法,该方法基于UserPaymentHistoryLog
的实例返回类的实例
一个静态的Parse
方法,该方法基于UserPaymentAccountHistoryLog
的实例返回类的实例
甲MergeWith
与实例合并现有实例实例方法UserPaymentHistoryLog
甲MergeWith
与实例合并现有实例实例方法UserPaymentAccountHistoryLog
例如:
public class CardTransactionHistoryWM
{
public bool ActionType { get; set; }
public bool TransactionType { get; set; }
public DateTime LogHistory { get; set; }
public string LogDetail { get; set; }
public OrderWM Order { get; set; }
public void MergeWith(UserPaymentHistoryLog uphl)
{
if (LogHistory != uphl.LogHistory)
throw new InvalidOperationException("Cannot merge if LogHistory dates don't match");
TransactionType = uphl.TransactionType;
LogDetail = string.Concat(LogDetail, uphl.LogDetail);
}
public void MergeWith(UserPaymentAccountHistoryLog upahl)
{
if (LogHistory != upahl.LogHistory)
throw new InvalidOperationException("Cannot merge if LogHistory dates don't match");
ActionType = upahl.ActionType;
LogDetail = string.Concat(LogDetail, upahl.LogDetail);
}
public static CardTransactionHistoryWM Parse(UserPaymentHistoryLog source)
{
return new CardTransactionHistoryWM
{
TransactionType = source.TransactionType,
LogHistory = source.LogHistory,
LogDetail = source.LogDetail
};
}
public static CardTransactionHistoryWM Parse(UserPaymentAccountHistoryLog source)
{
return new CardTransactionHistoryWM
{
ActionType = source.ActionType,
LogHistory = source.LogHistory,
LogDetail = source.LogDetail
};
}
}
添加了这些方法后,您就可以在解析两个列表时使用它们来创建CardTransactionHistoryWM
类的新实例,或者如果日期匹配则与现有实例合并:
public static List<CardTransactionHistoryWM> Merge(
List<UserPaymentAccountHistoryLog> userPaymentAccountHistoryLogs,
List<UserPaymentHistoryLog> userPaymentHistoryLogs)
{
var results = new List<CardTransactionHistoryWM>();
foreach (var upahl in userPaymentAccountHistoryLogs)
{
var match = results.SingleOrDefault(cthwm => cthwm.LogHistory == upahl.LogHistory);
if (match == null)
{
results.Add(CardTransactionHistoryWM.Parse(upahl));
}
else
{
match.MergeWith(upahl);
}
}
foreach (var uphl in userPaymentHistoryLogs)
{
var match = results.SingleOrDefault(cthwm => cthwm.LogHistory == uphl.LogHistory);
if (match == null)
{
results.Add(CardTransactionHistoryWM.Parse(uphl));
}
else
{
match.MergeWith(uphl);
}
}
return results;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.