使用ASP.NET和带有多个表的Linq2Sql的三层应用程序

本文关键字:Linq2Sql 应用程序 三层 NET ASP 使用 | 更新日期: 2023-09-27 17:58:09

大家好,我的三层应用程序有问题,我不知道如何在三层架构应用程序中使用linq2sql从多个表中获取数据这里是每个层的代码

GestionProjetCommon项目

客户端类别:

public class Client
{
    private int _ID;
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    private string _Name;
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

}

项目类别:

public class Projet
{
    private int _ID;
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    private string _Title;
    public string Title        {
        get { return _Title; }
        set { _Title= value; }
    }
   private int _IDClient;
    public int IDClient
    {
        get { return _IDClient; }
        set { _IDClient = value; }
    }
}

GestionProjetDAL项目

GestionProjetDA类别:

public class GestionProjetDA
{
    private GestionProjetDADataContext db = new GestionProjetDADataContext();
    public List<GestionProjet.Client> GetClients() //This Works Fine No Problem !
    {
        var req = from clt in db.Clients select clt;
        List<GestionProjet.Client> clientList = new List<GestionProjet.Client>();
        foreach (Clients item in req)
        {
            clientList.Add(new GestionProjet.Client() { ID = item.ID, Nom = item.Nom });
        }
        return clientList;
    }
public List<GestionProjet.Client> GetProjectClient()
    {
        var req = from prj in db.Projets
                  from clt in db.Clients
                  where clt.ID == prj.IDClient
                                  select new
                                  {
                                      Name=clt.Name,
                                      Projet = prj.Title,
                                  };
        List<GestionProjet.Client> clientProjectList = new List<GestionProjet.Client>();
      foreach (var clt in req)
        {
//I Don't know what to do in here and get the Data From both of the Tables
        }
    }
 }

GestionProjetBusiness项目

GestionProjetB类:

 public class GestionProjetB
{
    private GestionProjetDAL.GestionProjetDA GPDA = new GestionProjetDAL.GestionProjetDA();
    public List<Client> GetClients()
    {
        return GPDA.GetClients();
    }
  //Here i Should put the 2nd Method
}

正如你所看到的,我从一个表中获取数据没有问题,但唯一的问题是从多个表中获得数据。

我整晚都在寻找解决方案,但没有找到,请帮帮我感谢

使用ASP.NET和带有多个表的Linq2Sql的三层应用程序

创建一个DTO类,类似于:

public class ClientDTO
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string ProjectName { get; set; }
    }

现在编写一个好的linq表达式来填充DTO类:

 public List<GestionProjet.Client> GetProjectClient()
    {
        return (from prj in db.Projets
                  join clt in db.Clients on prj.IDClient equals clt.ID
                                  select new ClientDTO
                                  {
                                      Name = clt.Name,
                                      ProjetName = prj.Title,
                                      ID = clt.ID
                                  }).ToList();
    }

我希望我能正确理解你的问题,并原谅我在发布之前没有测试代码。