2 回答
TA贡献1827条经验 获得超7个赞
假设 DTO 为
public class Document
{
int Id { get; set; }
string DocumentName { get; set; }
bool IsNew { get; set; } // This field is not in the database
}
我可以使用这个事件处理程序:
private void Documents_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach(Document item in e.NewItems)
{
item.IsNew = true;
}
}
标记用户添加到数据网格的任何新记录。从数据库加载原始记录后,我钩住了这个处理程序:
public void LoadDocuments()
{
var documents = myIdbConnection.GetAll<Document>();
Documents = new ObservableCollection<Document>(documents);
Documents.CollectionChanged += Documents_CollectionChanged;
}
接着:
public void Save()
{
myIdbConnection.Update(Documents.Where(x=>!x.IsNew));
myIdbConnection.Insert(Documents.Where(x=>x.IsNew));
}
TA贡献1860条经验 获得超8个赞
您可以省去事件监听。只需使用另一个值对新记录进行默认初始化即可。
public class Document
{
static bool IsNewInitializer { get; set; } = false;
int Id { get; set; }
string DocumentName { get; set; }
bool IsNew { get; set; } = IsNewInitializer; // This field is not in the database
}
public void LoadDocuments()
{
Document.IsNewInitializer = false;
var documents = myIdbConnection.GetAll<Document>();
Documents = new ObservableCollection<Document>(documents);
Document.IsNewInitializer = true;
}
public void Save()
{
myIdbConnection.Update(Documents.Where(x => !x.IsNew));
myIdbConnection.Insert(Documents.Where(x => x.IsNew));
foreach (var d in Documents.Where(x => x.IsNew))
{
d.IsNew = false;
}
}
- 2 回答
- 0 关注
- 131 浏览
添加回答
举报