处理不直接映射到数据库列的属性
我有下表
客户端表 和 产品表
ID
Name
ClientProduct 表
ID
ClientID
ProductID
产品类
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
protected string name;
public Product () { }
public Product (string name)
{
this.name = name;
}
public Product (string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
客户端类
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
protected string name;
public Client () { }
public Client (string name)
{
this.name = name;
}
public Client (string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
ClientProduct 类
protected Client client;
protected Product product;
public ClientProduct () { }
public ClientProduct (Client client, Product product)
{
this.client= client;
this.product= product;
}
public Client client {
get { return client; }
set { client= value; }
}
public Product product {
get { return product; }
set { product= value; }
}
我如何在 petaPOCO 中执行以下操作?
public static System.Collections.Generic.IList<ClientProduct> LoadForClient(Client client)
{
if (null != client)
return Load("ClientID = " + client.ID);
else
return null;
}
这样我就可以获得该客户的所有产品的列表,稍后我将在我的视图中使用这些产品
private void LoadProducts(Client client )
{
Products = ClientProduct.LoadForClient(client)
.Select(x => x.Product.Name)
.OrderBy(x => x).ToArray();
}
I have the following tables
Client Table and Product Table
ID
Name
ClientProduct Table
ID
ClientID
ProductID
Product class
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
protected string name;
public Product () { }
public Product (string name)
{
this.name = name;
}
public Product (string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
Client class
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
protected string name;
public Client () { }
public Client (string name)
{
this.name = name;
}
public Client (string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
ClientProduct class
protected Client client;
protected Product product;
public ClientProduct () { }
public ClientProduct (Client client, Product product)
{
this.client= client;
this.product= product;
}
public Client client {
get { return client; }
set { client= value; }
}
public Product product {
get { return product; }
set { product= value; }
}
How can I do the following in petaPOCO?
public static System.Collections.Generic.IList<ClientProduct> LoadForClient(Client client)
{
if (null != client)
return Load("ClientID = " + client.ID);
else
return null;
}
such that I can have list of all products for that client that I will later used in my view as
private void LoadProducts(Client client )
{
Products = ClientProduct.LoadForClient(client)
.Select(x => x.Product.Name)
.OrderBy(x => x).ToArray();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1:M 和 M:1 关系似乎就是您所追求的
http://www .toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships
您可以定义自定义关系器 打回来; Brad 的两个表示例(如果您的产品直接映射到客户端)看起来像这样:
我意识到您处理的是 M:M 关系,因此您需要更新上面的内容以映射三个对象,但概念是相同。关键是调用中的第三个参数 (ClientProduct) 代表连接的行,然后您可以直接从单个列表引用客户和/或产品。
The 1:M and M:1 relationships seem like what you're after
http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships
You can define a custom relator callback; Brad's example for two tables (if your products mapped directly to the client) would look something like this:
I realize your dealing with a M:M relationship so you would need to update the above to map across the three objects, but the concept is the same. The key is that your 3rd argument in the call (ClientProduct) represents the joined row, and you can then reference the Client and/or Products directly from the single list.