6

我正在玩 Subsonic 3 的简单存储库,并且在理解如何处理外键方面遇到困难......

如果我有一个产品对象包含

int ID; 

string name; 

string description; 

Category category; 

int categoryID (this one is just to persist the product's categoryID to the DB)

and a category object containing 

int ID; 

string name;

我如何使用存储库来带回所有产品的列表及其类别对象实例化?

目前我已经编写了一个 linq 查询,它加入了 product.categoryID = category.ID 这一切都很好,但是当我 .ToList() 这个查询的结果时,产品的 Category 没有被实例化。

有没有办法做到这一点,或者我必须手动实例化每个产品的类别?

谢谢,

保罗

4

1 回答 1

6

您需要获取 linq 来填充它,
使用类似
var query = from product in repo.All(Product)
join categoryItem in repo.All(Category)
on product.CategoryId 等于 categoryItem.Id
select new {
ID = product.ID,
name = product.name,
description = product.description,
categoryId= product.CategoryId
category = categoryItem
};

于 2009-07-24T10:41:31.877 回答