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 等标签,如何设置查询?

4

2 回答 2

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 ;
}
于 2017-06-17T07:13:21.177 回答
-1

简单提示:

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

这里

YourPropertyName = with which property you want to match.

YourPropertyValue = Value of your property. 
于 2013-08-16T09:53:57.773 回答