C# 创建包含两个实体字段的新列表

C# 创建包含两个实体字段的新列表,c#,winforms,linq,lambda,C#,Winforms,Linq,Lambda,我有两个实体作为他们:员工和发送消息: public class Employee { [DbColumn(IsIdentity =true, IsPrimary =true)] public long EmployeeId { get; set; } [DbColumn] public string Name { get; set; } [DbColumn] public string Surname { get; set; } [DbC

我有两个实体作为他们:
员工
发送消息

public class Employee
{
    [DbColumn(IsIdentity =true, IsPrimary =true)]
    public long EmployeeId { get; set; }
    [DbColumn]
    public string Name { get; set; }
    [DbColumn]
    public string Surname { get; set; }
    [DbColumn]
    public string Date_Birth { get; set; }
    [DbColumn]
    public string Home_Address { get; set; }

    [DbColumn]
    public string City { get; set; }
    [DbColumn]
    public string Postcode { get; set; }
    [DbColumn]
    public string Telephone { get; set; }
    [DbColumn]
    public string Mobile { get; set; }
    [DbColumn]
    public string Email { get; set; }
    [DbColumn]
    public long ShiftId { get; set; }
}
如您所见,EmployeeId字段用于连接这两个字段

public class MessageSent
{
    [DbColumn(IsIdentity =true, IsPrimary =true)]
    public long MessageSentId { get; set; }
    [DbColumn]
    public long EmployeeId { get; set; }
    [DbColumn]
    public long MessageSentSeq { get; set; }
    [DbColumn]
    public string Status { get; set; }
    [DbColumn]
    public string DateSent { get; set; }

}
要兑换,我使用以下方法

gvEmployee.DataSource = new EmployeeService().GetAll();
现在我需要在新屏幕上显示以下字段:
messagesented表的
messagesented、EmployeeId、MessageSentSeq
,以及
员工表的
姓名、姓氏

如何创建包含这5个字段的第三个列表以填充我的表格?

以您的员工为例:

var employees = new EmployeeService().GetAll();
然后你的信息:

var messages = new MessageSentService().GetAll(); // probably like this, idk what its in your code
使用LINQ查询语法更清晰、更自然,更容易发现错误:

var query = 
    from employee in employees
    join message in messages
    on employee.EmployeeId equals message.EmployeeId 
    select new { 
        MessageSentId = message.MessageId,
        EmployeeId = message.EmployeeId,
        MessageSentSeq = message.MessageSentSeq,
        Name = employee.Name,
        Surname = employee.Surname
    };
然后您可以使用
查询
及其字段。

让您的员工:

var employees = new EmployeeService().GetAll();
然后你的信息:

var messages = new MessageSentService().GetAll(); // probably like this, idk what its in your code
使用LINQ查询语法更清晰、更自然,更容易发现错误:

var query = 
    from employee in employees
    join message in messages
    on employee.EmployeeId equals message.EmployeeId 
    select new { 
        MessageSentId = message.MessageId,
        EmployeeId = message.EmployeeId,
        MessageSentSeq = message.MessageSentSeq,
        Name = employee.Name,
        Surname = employee.Surname
    };

然后你可以使用
查询
及其字段。

@你会说你会像
newemployeeservice().GetAll()那样对待员工吗因此将其设置为变量:
var employees=new EmployeeService().GetAll()然后更改
数据库。员工
使用
员工
,对messages@Will更新的答案,希望这能引导你找到完美的解决方案!现在我用过滤器来满足我的需要。非常感谢@Mark@你会说你会接受像
newemployeeservice().GetAll()这样的员工吗因此将其设置为变量:
var employees=new EmployeeService().GetAll()然后更改
数据库。员工
使用
员工
,对messages@Will更新的答案,希望这能引导你找到完美的解决方案!现在我用过滤器来满足我的需要。非常感谢@Mark!