Pau*_*aul 6 .net c# subsonic repository subsonic3
我正在玩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;
Run Code Online (Sandbox Code Playgroud)
如何使用存储库返回实例化其类别对象的所有产品的列表?
目前我已经编写了一个连接在product.categoryID = category.ID上的linq查询,这一切都很好,但是当我.ToList()查询结果时,产品的类别没有实例化.
有没有办法做到这一点,还是我必须手动实例化每个产品的类别?
谢谢,
保罗
你需要让linq填充它,
使用
var query =来自repo.All(Product)中的产品
连接categoryItem in repo.All(Category)
on product.CategoryId equals categoryItem.Id
select new {
ID = product.ID,
name = product.name,
description = product.description,
categoryId = product.CategoryId
category = categoryItem
};