Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c# - Subsonic 3 - SimpleRepository

I am playing around with Subsonic 3's simple repository and am hitting walls in understanding how to deal with foreign keys...

If I have a product object containing

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;

How can I use the repository to bring back a list of all products with their category object instantiated?

At the moment I have written a linq query which joins on product.categoryID = category.ID which is all well and good, but when I .ToList() the results of this query, the product's Category isn't instantiated.

Is there a way to do this, or do I have to manually instantiate the Category for each product?

Thanks,

Paul

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you need to get linq to populate it,
using something like
var query = from product in repo.All(Product)
join 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
};


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...