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!