简体   繁体   English

不相关机型之间如何使用linq join? (其中之一是 IdentityUser)

[英]How to use linq join between unrelated models? (one of them is IdentityUser)

I use asp.net core EF.我使用 asp.net 核心 EF。
I have two models.我有两个模型。
[1] the first one is employee which inherits IdentityUser and extending it. [1] 第一个是继承 IdentityUser 并对其进行扩展的雇员。
[2] the second one is DocumentIn which has employee's key [2] 第二个是 DocumentIn,其中有员工的密钥

note: they are not related注意:它们不相关

the problem:问题:
I need to view all DocumentIn but with employee's names, not keys我需要查看所有 DocumentIn 但带有员工姓名而不是键

next are my two models respectively (with comments included to demonstrate Primary and Foreign keys)接下来分别是我的两个模型(包含注释以演示主键和外键)

Employee inheriting from IdentityUser从 IdentityUser 继承的员工

public class Employee: IdentityUser
{
    // this primary key (Id) is identified as a foreign key twice in the next model (Document In) 
    // but with different names 
    // the names are [EmpFrom] and [EmpTo]
    [Key] 
    public override string Id { get => base.Id; set => base.Id = value; }
    //-------------------------------------------------------------------------------------------

    [EmailAddress]
    public override string Email { get => base.Email; set => base.Email = value; }

    public string HomePhone { get; set; }

    public string EmpName { get; set; }

    public int? DivID { get; set; }
    [ForeignKey("DivID")]
    public Division division { get; set; }

    public string Password { get; set; }

    [NotMapped]
    public string ConfirmPassword { get; set; }

}

DocumentIn文档输入

public class DocumentIn
{
    [Key]
    public int DocInID { get; set; }

    public string DocName { get; set; } 
    public string Notes { get; set; }
    public string BarCode { get; set; }

    public DateTime ActionDate { get; set; } 
    public DateTime DueDate { get; set; } 

    // here I use these names to contains employees Id which is GUID of IdentityUser
    public string EmpFrom { get; set; } 
    public string EmpTo { get; set; } 

}

Important:重要的:

I use EF concept, DbContext to get DocumentIn我使用 EF 概念,DbContext 来获取 DocumentIn
however, I use UserManager on the other hand to get employees但是,另一方面,我使用 UserManager 来获取员工

like the following像下面这样

gitting DocumentIn using _context使用 _context gitting DocumentIn

// _TableView is a ViewModel which has this attripute 
// public List<DocumentIn> DocumentsInList { get; set; }

_TableView.DocumentsInList = await _context.DocumentIn.Where(bla bla bla).ToListAsync();

gitting employees using UserManager使用 UserManager 管理员工

 List<Employee> Emps = await userManager.Users.ToListAsync();

Now, I need to include Emp with DocumentsInList's query to read all documents with the names of employees, not with their Id.现在,我需要将 Emp 包含在 DocumentsInList 的查询中,以读取所有带有员工姓名的文档,而不是他们的 ID。
In other words, I need to view EmpName from Employee rather than EmpFrom and EmpTo in the above LINQ query.换句话说,我需要在上面的 LINQ 查询中从 Employee 中查看 EmpName 而不是 EmpFrom 和 EmpTo。

I already made the next LINQ query but I don't know how to replace EmpFrom and EmpTo from DocumentIn with EmpName from Employee我已经进行了下一个 LINQ 查询,但我不知道如何将 DocumentIn 中的 EmpFrom 和 EmpTo 替换为 Employee 中的 EmpName

  // -------- all users 
  List<Employee> Emps = await userManager.Users.ToListAsync();

  _TableView.DocumentsInList = await _context.DocumentIn.Include(e => Emps).ToListAsync();

I already made the next LINQ query but I don't know how to replace EmpFrom and EmpTo from DocumentIn with EmpName from Employee我已经进行了下一个 LINQ 查询,但我不知道如何将 DocumentIn 中的 EmpFrom 和 EmpTo 替换为 Employee 中的 EmpName

According to your Model code, since the DecumentIn model doesn't contain the reference navigation property for the Employee, if you want to display the Employee when query the DecumentIn table, you have to use the Join clause.根据您的 Model 代码,由于 DecumentIn model 不包含 Employee 的引用导航属性,如果要在查询 DecumentIn 表时显示 Employee,则必须使用 Join 子句。

And, you could create a view Model (such as: DocumentInViewModel) which contains a EmpName property, then in the LINQ select clause, you could create the DocumentInViewModel instance and display the Employee Name.并且,您可以创建一个包含 EmpName 属性的视图 Model(例如:DocumentInViewModel),然后在 LINQ select 子句中创建 DocumentViewModel 和显示实例名称。

Code as below (Suppose the DocumentIn's EmpFrom value equals the Emplyee's ID value):代码如下(假设 DocumentIn 的 EmpFrom 值等于 Emplyee 的 ID 值):

public class DocumentInViewModel
{ 
    public int DocInID { get; set; }

    public string DocName { get; set; } 
    public string Notes { get; set; }
    public string BarCode { get; set; }

    public DateTime ActionDate { get; set; } 
    public DateTime DueDate { get; set; } 

    // used to display the Employee Name.
    public string EmpName { get; set; }  

}

Then, you could try to use the following code to query related data.然后,您可以尝试使用以下代码查询相关数据。

        List<Employee> Emps = await userManager.Users.ToListAsync();
        var resultee = _context.DocumentIn.ToList()
            .Join(Emps, d => d.EmpFrom, e => ((Employee)e).Id, (d, e)=> new { DocumentIn= d, Employee = e })
            .Select(c=> new DocumentInViewModel { DocName = c.DocumentIn.DocName, EmpName = c.Employee.EmpName })
            .ToList();

More detail information about the Linq Join statement, you could check this thread .有关 Linq Join 语句的更多详细信息,您可以查看此线程

Why dont you create a view in the db and use that to show both models as per your requirement为什么不在数据库中创建一个视图并根据您的要求使用它来显示两个模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM