【问题标题】:Entity Framework Query for Multiple Tag Cloud related Posts多个标签云相关帖子的实体框架查询
【发布时间】:2013-08-16 09:07:16
【问题描述】:

如果我有像

这样的模型类
[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 等标签,如何设置查询?

【问题讨论】:

  • 请您详细说明选择所有匹配的帖子?这里的匹配是基于什么条件的?
  • 我想选择所有匹配标签的帖子,例如车辆或电子产品。
  • “车辆或电子产品”是什么意思?标签名称??

标签: c# .net entity-framework tags entity


【解决方案1】:

您有汽车、手机、车辆、电子产品和...等标签。

当您添加帖子时,您会添加一些标签。像这个网站(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 ;
}

【讨论】:

    【解决方案2】:

    简单提示:

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

    这里

    YourPropertyName = with which property you want to match.
    
    YourPropertyValue = Value of your property. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多