1

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

GestionProjet通用项目

客户类别:

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
        }

    }
 }

GestionProjet商业项目

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

}

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

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

4

1 回答 1

3

创建一个 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();

    }

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

于 2011-06-02T12:29:04.147 回答