本文介绍了在一个datagrid中加入两个表usig Linq C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个表连接到一个数据网格中。表一: tblProjects - 表二: tblEmployeeLoginDetails



在此方法中 - private void FillProjectsDataGrid()我想使用这个编码填充我的datagrid:

  dgViewProjects.ItemsSource = DC。 tblProjects.Where< tblProject>(c => c.ProjectID!= null)
.Select< tblProject,ProjectData>(m => new ProjectData()
{
ProjectID = m .ProjectID,
Name = m.ProjectName,
Status = m.ProjectStatus,
Employee = m.EmployeeName,
});

dgViewProjects.ItemsSource = DC.tblEmployeeLoginDetails.Where< tblEmployeeLoginDetail>(c => c.LoginID!= null)
.Select< tblEmployeeLoginDetail,EIDData>(m => new EIDData ()
{
UserID = m.LoginID,
Name = m.EmployeeName,
Surname = m.EmployeeSurname,
Email = m.EmployeeEmailAddress,
Password = m.EmployeePassword,
Role = m.RoleID.ToString(),
Department = m.EmployeeDepartment,
IDNumber = m.EmployeeIDNumber,
Gender = m.EmployeeGender ,
Date = m.EmployeeDOB.Value,
HomeAddress = m.EmployeeHomeAddress,
电话= m.EmployeeTelephoneNumber,
City = m.EmployeeCity,
省= m .EmployeeProvinceCode,
SetImage = m.EmployeeProfilePicture
});

对于 tblProjects ,我使用这个类:

  public struct PDData 
{
public string _project;

public int ProjectID {get;组; }
public string Name {get;组; }
public string Status {get;组; }
public string Employee {get;组;
}

对于 tblEmployeeLoginDetails 我使用这个类: / p>

  public struct EIDData 
{
public string _sts;

public int UserID {get;组; }
public string Name {get;组; }
public string Surname {get;组; }
public string Email {get;组; }
public string Password {get;组; }
public string Role {get;组; }
public string Department {get;组; }
public string IDNumber {get;组; }
public string Gender {get;组; }
public DateTime Date {get;组; }
public string HomeAddress {get;组; }
public string电话{get;组; }
public string City {get;组; }
public string省{get;组; }
public Binary SetImage {get;组; }
}

现在我想加入这两个表,并在一个datagrid中显示信息。我尝试了一个Linq join语句,但是我收到这个错误:

最后这是我的连接编码:

 列表< tblProject>在DC.tblEmployeeLoginDetails中从u中加入=(List< tblProject>)
在DC.tblProjects中连接b $ u
在u.LoginID等于b.ProjectID
其中b.ProjectID!= null
选择b;

我不知道如何使用我必须显示的类加入这两个表一个datagrid。如果有任何建议或帮助,请随时留言。谢谢!

解决方案

这将为您提供一个新的匿名类,同时加入您的员工和项目详细信息。你拥有的加入只是为项目返回值

  var Join =(from u in DC.tblEmployeeLoginDetails 
join b在DC.tblProjects
on u.LoginID等于b.ProjectID
其中b.ProjectID!= null
选择新的{Project = a,Employee = b})ToList();

然后,您必须将其绑定到您的datatable,如下所示:

  dgViewProjects.ItemsSource = Join; 

您的第一个代码块将覆盖第二个数据源的初始设置,而不是添加


I want to join two tables into one datagrid. table one: tblProjects - table two: tblEmployeeLoginDetails

In this method- private void FillProjectsDataGrid() I want to fill my datagrid using this coding:

            dgViewProjects.ItemsSource = DC.tblProjects.Where<tblProject>(c => c.ProjectID != null)
                .Select<tblProject, ProjectData>(m => new ProjectData()
                {
                    ProjectID = m.ProjectID,
                    Name = m.ProjectName,
                    Status = m.ProjectStatus,
                    Employee = m.EmployeeName,
                });

            dgViewProjects.ItemsSource = DC.tblEmployeeLoginDetails.Where<tblEmployeeLoginDetail>(c => c.LoginID != null)
                .Select<tblEmployeeLoginDetail, EIDData>(m => new EIDData()
                {
                    UserID = m.LoginID,
                    Name = m.EmployeeName,
                    Surname = m.EmployeeSurname,
                    Email = m.EmployeeEmailAddress,
                    Password = m.EmployeePassword,
                    Role = m.RoleID.ToString(),
                    Department = m.EmployeeDepartment,
                    IDNumber = m.EmployeeIDNumber,
                    Gender = m.EmployeeGender,
                    Date = m.EmployeeDOB.Value,
                    HomeAddress = m.EmployeeHomeAddress,
                    Telephone = m.EmployeeTelephoneNumber,
                    City = m.EmployeeCity,
                    Province = m.EmployeeProvinceCode,
                    SetImage = m.EmployeeProfilePicture
                });

For tblProjects I use this class:

public struct PDData
{
    public string _project;

    public int ProjectID { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
    public string Employee { get; set; }
}

For tblEmployeeLoginDetails I use this class:

public struct EIDData
{
    public string _sts;

    public int UserID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Role { get; set; }
    public string Department { get; set; }
    public string IDNumber { get; set; }
    public string Gender { get; set; }
    public DateTime Date { get; set; }
    public string HomeAddress { get; set; }
    public string Telephone { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public Binary SetImage { get; set; }
}

Now I want to join these two tables and display the information in one datagrid. I have tried a Linq join statement but I get this error:

And lastly here is my join coding:

        List<tblProject> Join = (List<tblProject>)from u in DC.tblEmployeeLoginDetails
                                                  join b in DC.tblProjects
                                                  on u.LoginID equals b.ProjectID
                                                  where b.ProjectID != null
                                                  select b;

I have no idea how to join these two tables together using the classes that I have to display it in one datagrid. If anyone has any advice or help, please don't hesitate to leave a comment. Thank you!

解决方案

This will give you a new anonymous class with both the employee and project details from your join. The join you have was only returning values for the projects

    var Join = (from u in DC.tblEmployeeLoginDetails
                                              join b in DC.tblProjects
                                              on u.LoginID equals b.ProjectID
                                              where b.ProjectID != null
                                              select new {Project=a, Employee=b}).ToList();

Then you would have to bind this to your datatable, something like this:

    dgViewProjects.ItemsSource = Join;

Your first block of code is going to overwrite the initial setting of the datasource with the second, not add to it.

这篇关于在一个datagrid中加入两个表usig Linq C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:11