以下是我的实体之一:
conda install -c r r-microbenchmark
这是我的另一个实体:
[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
linq查询,但是那些相同的属性会让我感到困惑。
重要的是,如果zip
和UserPaymentAccountHistoryLog
具有完全相同的日期记录,则UserPaymentHistoryLog
应该为此日期发布一行,而不是两行,并同时显示{{1 }}和CardTransactionHistoryWM
LogDetails。
提前谢谢!
答案 0 :(得分:0)
如果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;
}