使用Lambda表达式在多对多关系表中实现查询 [英] using Lambda expression for implementing a query in many-to-many relation tables

查看:653
本文介绍了使用Lambda表达式在多对多关系表中实现查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据图片,我设计了3个表,分别是Article,Tag和ArticleTag。
ArticleTag是文章和标签表之间的关联表。
现在,我想使用Lambda表达式创建一个根据TagId获取文章的查询。



我已经使用了Entity Framework 6.x
首先,我通过一个查询获取了TagId根据tagTitle。

  // tagTitle是一个传递给一个方法的参数。 
// isActiveTag是传递给方法的参数。
int currentTagId = DataContextFactory.GetDataContext()
.Tags.Where(p => p.Title == tagTitle&& p.IsActive == isActiveTag)
.Select(p => p.Id).SingleOrDefault();

现在我想使用currentTagId获取与之相关的文章。



如何通过 Lambda表达式创建一个根据currentTagId获取文章的查询?

  public class标签:Entity,ITag 
{
public string Title {get;组; }
public string描述{get;组; }
public bool? IsActive {get;组; }
public virtual ISet< ArticleTag> ArticleTags {get;组; }
public virtual ISet< ProjectTag> ProjectTags {get;组;
}
public class Article:Entity,IArticle
{
public virtual int? UserMemberShipId {get;组; }

public virtual string标题{get;组; }

public virtual string Summary {get;组; }

public virtual string说明{get;组;

公开虚拟十进制? RateCounter {get;组; }

public virtual int? LikeCounter {get;组; }

public virtual bool IsActive {get;组; }

public virtual bool IsActiveNewComment {get;组; }

public virtual ISet< Comment>评论{get;组; }

public virtual ISet< Rating>评分{get;组; }

public virtual ISet< AttachmentFile> AttachmentFiles {get;组; }

public virtual ISet< ArticleTag> ArticleTags {get;组; }

public virtual ISet< ArticleLike> ArticleLikes {get;组;
}

public class ArticleTag:Entity,IArticleTag
{
public virtual int TagId {get;组; }
public virtual int ArticleId {get;组;
public virtual Article Article {get;组; }
public virtual Tag Tag {get;组; }
public virtual int?额外{get;组; }
}

解决方案

,所以你应该能够通过他们的键加入其他表,并选择匹配的文章记录。



尝试这样:(我不能测试它,当然,但看看它是否有效)

  var context = DataContextFactory.GetDataContext(); 

var articles =(来自context.Tags中的标签
在上下文中加入ta。tag.Id上的ArticleTags等于ta.TagId
在上下文中加入文章。 equals article.Id
其中tag.Title == tagTitle
&&& tag.IsActive == isActiveTag
select article).ToList();


According to the picture, I've designed 3 tables that are Article, Tag and ArticleTag. ArticleTag is a associated table between Article and Tag table. Now, I want to use Lambda expression to create a query that obtains articles according to TagId.

I've used Entity Framework 6.x First I got TagId according to tagTitle by a query.

// tagTitle is a parameter that passed to a method.
// isActiveTag is a parameter that passed to a method.
int currentTagId = DataContextFactory.GetDataContext()
            .Tags.Where(p => p.Title == tagTitle && p.IsActive == isActiveTag)
            .Select(p => p.Id).SingleOrDefault();

Now I want to use currentTagId for getting the Articles that related to it.

How can I create a query by Lambda expression that gets Articles according to the currentTagId?

public class Tag : Entity, ITag
{
    public string Title { get; set; }
    public string Description { get; set; }
    public bool? IsActive { get; set; }
    public virtual ISet<ArticleTag> ArticleTags { get; set; }
    public virtual ISet<ProjectTag> ProjectTags { get; set; }
}
public class Article : Entity, IArticle
{
    public virtual int? UserMemberShipId { get; set; }

    public virtual string Title { get; set; }

    public virtual string Summary { get; set; }

    public virtual string Description { get; set; }

    public virtual decimal? RateCounter { get; set; }

    public virtual int? LikeCounter { get; set; }

    public virtual bool IsActive { get; set; }

    public virtual bool IsActiveNewComment { get; set; }

    public virtual ISet<Comment> Comments { get; set; }

    public virtual ISet<Rating> Ratings { get; set; }

    public virtual ISet<AttachmentFile> AttachmentFiles { get; set; }

    public virtual ISet<ArticleTag> ArticleTags { get; set; }

    public virtual ISet<ArticleLike> ArticleLikes { get; set; }
}

public class ArticleTag : Entity, IArticleTag
{
    public virtual int TagId { get; set; }
    public virtual int ArticleId { get; set; }
    public virtual Article Article { get; set; }
    public virtual Tag Tag { get; set; }
    public virtual int? Extra { get; set; }
}

解决方案

You're already filtering by the title you want, so you should be able to just join the other tables by their keys and select the matching "article" records.

Try this: (I can't test it of course, but see if it works)

var context = DataContextFactory.GetDataContext();

var articles = (from tag in context.Tags
                join ta in context.ArticleTags on tag.Id equals ta.TagId
                join article in context.Articles on ta.ArticleId equals article.Id
                where tag.Title == tagTitle
                   && tag.IsActive == isActiveTag
                select article).ToList();

这篇关于使用Lambda表达式在多对多关系表中实现查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆