使用LINQ-To-Entities生成信息

时间:2010-05-20 19:39:09

标签: c# .net asp.net-mvc linq linq-to-entities

我正在开发一个网站,用户可以在其网站上添加标签,就像目前针对Stack Overflow上的问题所做的那样。

Books
{
bookId,
Title
}

Tags
{
Id
Tag
}

BooksTags
 {
 Id
 BookId
 TagId
 }

以下是一些示例记录。

Books
BookId Title
113421  A
113422  B

Tags
Id Tag
1  ASP 
2  C#
3  CSS
4  VB
5  VB.NET
6  PHP
7  java
8  pascal

 BooksTags   
 Id  BookId  TagId
 1  113421    1
 2  113421    2
 3  113421    3
 4  113421    4
 5  113422    1
 6  113422    4
 7  113422    8

问题

  1. 我需要在LINQ中写一些实体查询,根据标签给出数据:

    查询bookIds where tagid = 1
    退货bookid: 113421, 113422

    查询2 tags 1 and 2
    退货113421

  2. 我需要标签及其计数才能在相关标签中显示,所以在第一种情况下 我的相关标签类应该有以下结果。

    RelatedTags 标签计数 2 1 3 1 4 2 8 1

  3. 第二个案例

    RelatedTags
    Tag Count
    3   1
    4   1
    

    如何在LINQ中执行此操作?

3 个答案:

答案 0 :(得分:0)

只需将外键映射为1:N或1:1关系的表,然后让设计人员为您创建导航属性。 (书籍:BooksTags将Books.BookID中的1:N映射到BooksTags.BookID,而BooksTags.TagID将1:1映射到Tags.TagID)。这实际上是伪装的N:M关系。我不知道设计师是否直接选择了这个,但是通过一些摆弄你可以获得正确的导航属性。

现在提出问题:

  1. model.Tags.Where(t => t.ID == 1).Books.Select(b => b.ID)

  2. 获取图书的所有标签,并将该表加入BooksTags,通过这种方式,您只需使用Count()即可获得计数。

答案 1 :(得分:0)

在第一部分,有趣的限制是书必须匹配输入的每个标签,因此“where tagid == someId”的where子句不会真正起作用。我想象这样的事情(LINQ到对象的例子)

List<int> selectedTagIds = new List<int>() { 1, 2 };
var query = from book in books
            join booktag in booktags 
            on book.Id equals booktag.BookId 
            join selectedId in selectedTagIds 
            on booktag.TagId equals selectedId 
            group book by book into bookgroup 
            where bookgroup.Count() == selectedTagIds.Count
            select bookgroup.Key;

这基本上执行从书籍到书签以及所选标签ID列表的连接,并将选择限制为book-&gt;标签匹配的数量等于所选标签ID的数量。

要提取相关标签,可能是这样的

var relatedTags = from book in query // use original query as base
                    join booktag in booktags
                    on book.Id equals booktag.BookId
                    join tag in tags
                    on booktag.TagId equals tag.Id
                    where !selectedTagIds.Contains(tag.Id) // exclude selected tags from related tags
                    group tag by tag into taggroup
                    select new
                    {
                        Tag = taggroup.Key,
                        Count = taggroup.Count()
                    };

快速示例的完整代码。不完全是OOP,但你明白了。

using System;
using System.Collections.Generic;
using System.Linq;

namespace StackOverflow
{
    class Program
    {
        static void Main()
        {
            List<Book> books = new List<Book>() 
            {
                new Book() { Id = 113421, Title = "A" },
                new Book() { Id = 113422, Title = "B" }
            };

            List<Tag> tags = new List<Tag>()
            {
                new Tag() { Id = 1, Name = "ASP" },
                new Tag() { Id = 2, Name = "C#" },
                new Tag() { Id = 3, Name = "CSS" },
                new Tag() { Id = 4, Name = "VB" },
                new Tag() { Id = 5, Name = "VB.NET" },
                new Tag() { Id = 6, Name = "PHP" },
                new Tag() { Id = 7, Name = "Java" },
                new Tag() { Id = 8, Name = "Pascal" }
            };

            List<BookTag> booktags = new List<BookTag>()
            {
                new BookTag() { Id = 1, BookId = 113421, TagId = 1 },
                new BookTag() { Id = 2, BookId = 113421, TagId = 2 },
                new BookTag() { Id = 3, BookId = 113421, TagId = 3 },
                new BookTag() { Id = 4, BookId = 113421, TagId = 4 },
                new BookTag() { Id = 5, BookId = 113422, TagId = 1 },
                new BookTag() { Id = 6, BookId = 113422, TagId = 4 },
                new BookTag() { Id = 7, BookId = 113422, TagId = 8 }
            };


            List<int> selectedTagIds = new List<int>() { 1,2 };

            // get applicable books based on selected tags

            var query = from book in books
                        join booktag in booktags
                        on book.Id equals booktag.BookId
                        join selectedId in selectedTagIds
                        on booktag.TagId equals selectedId
                        group book by book into bookgroup
                        where bookgroup.Count() == selectedTagIds.Count
                        select bookgroup.Key;

            foreach (Book book in query)
            {
                Console.WriteLine("{0}\t{1}",
                    book.Id,
                    book.Title);
            }

            // get related tags for selected tags

            var relatedTags = from book in query // use original query as base
                              join booktag in booktags
                              on book.Id equals booktag.BookId
                              join tag in tags
                              on booktag.TagId equals tag.Id
                              where !selectedTagIds.Contains(tag.Id) // exclude selected tags from related tags
                              group tag by tag into taggroup
                              select new
                              {
                                  Tag = taggroup.Key,
                                  Count = taggroup.Count()
                              };

            foreach (var relatedTag in relatedTags)
            {
                Console.WriteLine("{0}\t{1}\t{2}",
                    relatedTag.Tag.Id,
                    relatedTag.Tag.Name,
                    relatedTag.Count);
            }

            Console.Read();
        }
    }

    class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }

    class Tag
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    class BookTag
    {
        public int Id { get; set; }
        public int BookId { get; set; }
        public int TagId { get; set; }
    } 
}

因此对于选定的标签1&amp; 2,你会得到书A,相关的标签是3(CSS)和4(VB)。

答案 2 :(得分:0)

这与答案没有直接关系,但您可能需要查看linqpad,因为它可以帮助您直接从数据库构建L2S语句。