大家好我的3层应用程序有问题我不知道如何使用linq2sql在3层架构应用程序中从多个表中获取数据这里是每个层代码
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 Project
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
}
你可以看到我从一个表中获取数据没有问题,但唯一的问题是从多个表中获取数据。
我整晚都在寻找解决方案,但我没有找到它请帮助我 感谢
答案 0 :(得分: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();
}
我希望我理解你的问题,请原谅我在发布前不测试代码。