簡體   English   中英

C# Linq 將 2 個 ObservableCollections 加入新的一個

[英]C# Linq Join 2 ObservableCollections into new one

我有 3 個類(T1 和 T2 數據來自 SQLite 文件)

T1 (EmployeeEntity --> Id, FirstName, LastName, DepartmentFK) << DepartmentKey 是 T2.Id 的外鍵

T2(部門實體-> ID,部門)

T3 (DepartmentEmployee --> Id, FirstName, LastName, DepartmentFK, Department) << Id 應該與 T1.Id 相同(使保存更改更容易)

在我的視圖模型中,我創建了一個新的 ObservableCollection public ObservableCollection<DepartmentEmployee> EmployeesView並希望用 2 個 SQLite 表中的數據填充這些數據,這就是我的做法:

    get
    {
        var t1 = new ObservableCollection<EmployeeEntity>(_emp.GetAll().ToObservable());
        var t2 = new ObservableCollection<DepartmentEntity>(_dep.GetAll().ToObservable());
        var result = t1.Join(t2, t => t.DepartmentFK, t => t.Id, (EmployeeEntity, DepartmentEntity) =>
        {
            var t3 = _mapper.Map<EmployeeEntity, DepartmentEmployee>(EmployeeEntity);
            t3 = _mapper.Map(DepartmentEntity, t3);
            return t3;
        });
        _employeesview = result.ToObservable();
        return _employeesview;
    }

這適用於我的視圖(我可以顯示名字、姓氏和適當的部門(基於外鍵))但是當我嘗試保存我的更改時,它會更改錯誤的記錄。 我的問題是這樣的:

T1

1 - Test - User - 2
2 - Another - User -3
3 - Third - User - 1

T2

1 - Development
2 - HR
3 - Production

導致這個

T3

2 - Test - User - 2 - HR
3 - Another - User - 3 - Production
1 - Third - User - 1 - Development

所以它“殺死”了我的員工 ID 並將其替換為部門的 ID。 那么我該如何解決這個問題呢?


添加。 信息:這些是我制作的映射

        cfg.CreateMap<DepartmentEntity, Departments>();
        cfg.CreateMap<Departments, DepartmentEntity>();
        cfg.CreateMap<EmployeeEntity, DepartmentEmployee>();
        cfg.CreateMap<DepartmentEmployee, EmployeeEntity>();
        cfg.CreateMap<DepartmentEntity, DepartmentEmployee>();

愚蠢的我,我不得不在我的映射中忽略“Id”

-->

cfg.CreateMap<DepartmentEntity, DepartmentEmployee>().ForMember(dest => dest.DepartmentFK, opt => opt.MapFrom(src => src.Id)).ForMember(dest => dest.Id, act => act.Ignore());

成功了:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM