Search code examples
c#asp.netlinq-to-sql3-tier

3-tiers application using ASP.NET and Linq 2 Sql with multiple tables


hello everyone i have a problem with my 3-tiers application i don't know how to get data from multiple tables using linq2sql in 3 tiers architecture application here is each layers code

GestionProjetCommon Project

Client Class :

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; }
    }

}

Project Class :

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 Project

GestionProjetDA Class :

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 Project

GestionProjetB Class :

 public class GestionProjetB
{
    private GestionProjetDAL.GestionProjetDA GPDA = new GestionProjetDAL.GestionProjetDA();

    public List<Client> GetClients()
    {
        return GPDA.GetClients();
    }

  //Here i Should put the 2nd Method

}

Well as you can see i have no problem with getting data from one table but the only problem is getting it from multiple tables.

i've been look for a solution the whole night but i didn't find it please help me thanks


Solution

  • Create a DTO class, something like:

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

    now write a good linq expression to fill the DTO class:

     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();
    
        }
    

    I hope I understood your problem right, and pardon me for not testing the code before posting.