[英]Updating list changed items
Currently to update a list I delete all the items and insert everything from scratch.目前要更新列表,我会删除所有项目并从头开始插入所有内容。 Aside from being inefficient, it is making the audit logs unreadable especially when the lists are large.
除了效率低下之外,它还使审计日志不可读,尤其是在列表很大时。
In my app service I update the list as follows:在我的应用服务中,我更新列表如下:
private async Task UpdateCourseTrainers(Course courseUpdateInputs)
{
await _courseTrainerRepository.DeleteAsync(cd => cd.CourseId == courseUpdateInputs.Id);
foreach (var trainer in courseUpdateInputs.CourseTrainers)
{
await _courseTrainerRepository.InsertAsync(trainer);
}
}
I imagine it would look something like this:我想它看起来像这样:
private async Task UpdateCourseTrainers(Course courseInputEntity, Course savedCourse)
{
var toInsert = courseInputEntity.CourseTrainers.Except(savedCourse.CourseTrainers).ToList();
var toRemove = savedCourse.CourseTrainers.Except(courseInputEntity.CourseTrainers).ToList();
await _courseTrainerRepository.DeleteAsync(???)
foreach (var trainer in toInsert)
{
await _courseTrainerRepository.InsertAsync(trainer);
}
}
Any help would be appreciated.任何帮助,将不胜感激。 Thanks
谢谢
private async Task UpdateCourseTrainers(Course courseInputEntity, Course savedCourse)
{
var toRemove = savedCourse.CourseTrainers.Except(courseInputEntity.CourseTrainers).ToList();
await _courseTrainerRepository.DeleteAsync(ct => toRemove.Any(t => t.TrainerId == ct.TrainerId));
var toInsert = courseInputEntity.CourseTrainers.Except(savedCourse.CourseTrainers).ToList();
foreach (var trainer in toInsert)
{
await _courseTrainerRepository.InsertAsync(trainer);
}
}
And then in the CourseTrainer class I implemented IEquatable and added the following code to allow the Except above to work然后在 CourseTrainer class 我实现了 IEquatable 并添加了以下代码以允许上述除外工作
public bool Equals(CourseTrainer other)
{
if (ReferenceEquals(other, null))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return CourseId.Equals(other.CourseId) && TrainerId.Equals(other.TrainerId);
}
public override int GetHashCode()
{
int hashCourseId = CourseId.GetHashCode();
int hashTrainerId = TrainerId.GetHashCode();
return hashCourseId ^ hashTrainerId;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.