LINQ查询标签系统 - 搜索多个标签 [英] linq query for tag system - search for multiple tags

查看:143
本文介绍了LINQ查询标签系统 - 搜索多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个表,标签(标签识别,帖子ID,标记名)和帖子(帖子ID,姓名,......)

现在我想让它返回我,有标签的通用量的所有帖子的查询。
这样的:我想,有标记asp.net和所有帖子的jQuery

Hi
I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...)
now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have the tag asp.net AND jquery

正如我所说的,标签查找量一般

as i said, the amount of tags to look for is generic

我怎么可以这样做呢?

THX

更新2009年11月17日:
有一个问题:betwenn表之间的关系是不存在的,因为我的主键是2字段(版本),我怎样才能使它没有关系?
使用LINQ到实体林

update 17.11.2009: there is one problem: the relation betwenn the tables does not exist, because my primary key is on 2 fields (for versioning) how can i make it without a relation? Im using Linq To Entities

此外,查询应该有不错的表现,而不应使成千上万的服务器的请求。

also, the query should have good performance, and should not make thousands of server requests.

推荐答案

我想使用EF 3.5,我有一个问题,你不能使用。载有()。我周围这让使用这个其中扩展

I guess you can't use .Contains() using EF 3.5 which I have had a problem with. I got around this by using this "WhereIn" extension

<一个href=\"http://stackoverflow.com/questions/374267/contains-workaround-using-linq-to-entities\">http://stackoverflow.com/questions/374267/contains-workaround-using-linq-to-entities

放入一个静态类本,那么你可以使用这样的:

put this into a static class then you could use something like:

IQueryable<Post> PostsWithByTags(IEnumerable<string> tagNames)
{
    var postIds = context.Tags.Select(t=>t.postid); 

    foreach (var tag in tagNames)
    {
       postIds = context.Tags
                     .WhereIn(t=> t.postid, postIds)
                     .Where(t=>t.tagname == tag);
    }

    return context.Tags.Where( t=> t.posId, postIds)
}

我真的觉得你应该看看有你的表之间的关系虽然。

I really think you should look at having relationships between your tables though.

这篇关于LINQ查询标签系统 - 搜索多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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