我的申请结构如下:
namespace DomainModel.Abstract
{
public interface IContentItemsRepository
{
IQueryable<Content> ContentItems { get; }
IQueryable<Category> Categories { get; }
IQueryable<ContentCategory> ContentCategories { get; }
}
}
namespace DomainModel.Entities
{
[Table(Name = "Content")]
public class Content
{
[Column(IsPrimaryKey = true,
IsDbGenerated = true,
AutoSync = AutoSync.OnInsert)]
public int content_id { get; set; }
[Column]
public string type { get; set; } // article, video, recipe, tool
[Column]
public string title { get; set; }
...
namespace DomainModel.Entities
{
[Table(Name = "Content_Categories")]
public class ContentCategory
{
[Column(IsPrimaryKey = true,
IsDbGenerated = true,
AutoSync = AutoSync.OnInsert)]
public int content_category_id { get; set; }
[Column]
public int content_id { get; set; }
[Column]
public int category_id { get; set; }
...
namespace DomainModel.Entities
{
[Table(Name = "Categories")]
public class Category
{
[Column(IsPrimaryKey = true,
IsDbGenerated = true,
AutoSync = AutoSync.OnInsert)]
public int category_id { get; set; }
[Column]
public string category_name { get; set; }
[Column]
public string type { get; set; } //recipe,tool,other
[Column]
public int ordering { get; set; }
...
我可以这样做:
var articlesInCategory = _contentRepository.ContentItems
.Where(x => x.type == "article");
并获取文章列表。没问题。
但是,我现在需要根据类别选择内容。因此,我需要将Content to ContentCategory加入Category。
我不知道该怎么做。任何帮助将不胜感激。
感谢。
编辑:
我认为我的部分问题是我甚至不知道如何调用我正在做的事情,所以很难搜索到这一点。我甚至做LINQ to SQL,LINQ to Entities,还是LINQ to Objects?
答案 0 :(得分:1)
您正在寻找的概念在linq中称为SelectMany,并且有许多方法可以实现它。
一个是:
var content =
from category in _categoryRepository.CategoryItems
join contCat in _contentCategoryRepository.Items
on category.category_id == conCat.category_id
where category.category_id == parameter
select contCat.content_id;
从这里开始,您应该可以将其扩展为提取所需的所有数据...查看into
关键字并查看this link(如果您还没有)。
答案 1 :(得分:1)
联接查询将是这样的。
var content=
from category in _contentRepository.Category
join contentCategory in _contentRepository.ContentCategory
on category.category_id equals contentCategory.category_id
join content in _contentRepository.Content
on content.content_id equals contentCategory.content_id
where category.category_id==@yourcategoryId
select new {type , title }