2013-08-16 155 views
1

如果我有一個像實體框架查詢的多標籤雲相關的帖子

[Table("MTag")] 
public class Tag 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int TagId { get; set; } 
    public string TagLabel { get; set; } 
    public virtual ICollection<TagRef> RefTags { get; set; } 
} 

[Table("TagRef")] 
public class TagRef 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int TagRefId { get; set; } 
    public virtual Tag Tag { get; set; } 
    public virtual ICollection<Post> Posts { get; set; } 
} 

[Table("Post")] 
public class Post 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int PostId { get; set; } 
    public UserProfile User { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public string PostGuid { get; set; } 
    public string Make { get; set; } 
    public string Model { get; set; } 
    public virtual ICollection<MTagRef> Tags { get; set; } 
    public string ImageFileName { get; set; } 
    public int Price { get; set; } 
    public int ImageWidth { get; set; } 
    public int ImageHeight { get; set; } 

} 

什麼是可能的查詢選擇所有匹配的崗位模型類?你能給我一個提示,如果我有像Car,Mobile這樣的標籤如何設置查詢嗎?

+0

能不能請您詳細說明更多*選擇所有匹配的職位*?這裏的匹配是基於哪些條件? –

+0

匹配到什麼? –

+0

我想選擇所有與車輛或電子設備相匹配的標籤。 –

回答

-1

簡單的提示:

var tags = db.Post.Where(m=>m.YourPropertyName == "YourPropertyValue").ToList(); 

這裏

YourPropertyName = with which property you want to match. 

YourPropertyValue = Value of your property. 
0

你有一個像汽車,手機,汽車,電子和標籤......。

當你添加一個帖子,你添加一些標籤給它。像這個網站(Stackoverflow),當你添加一個問題,你也添加一些標籤。現在,您要選擇帶有指定標籤的所有帖子。

以下方法返回的所有帖子有一些標籤:

public static IQueryable<Post> PostsWithTags(List<int> tagIds) 
{ 
     Context c = new Context(); 
     var Query = (from Group in c.TagRefs.GroupBy(g => g.TagId) let GroupTags = Group.Select(g => g.TagId) where tagIds.All(gt => GroupTags.Contains(gt)) select Group.Select(g => g.Post).FirstOrDefault()); 

     return Query ; 
}