复杂的LINQ查询,连接,组和总和

时间:2013-11-26 19:01:48

标签: c# linq

我有这些课程:

Book { UidBook, UidCategory, Score }  
Category { UidCategory, Score }  
Author { UidAuthor, Score }  
AuthorBook { UidAuthor, UidBook }  

这些是Lists<>组织的,如您所见,书与作者之间存在多对多的关系。

我想要做的是使用linq到select new { UidBook, Score },其中得分是书籍,类别得分和属于书籍的每位作者的得分之和。

我一直在努力解决这个问题,但我的大脑奇怪地停止了工作。

3 个答案:

答案 0 :(得分:2)

这可能对您有所帮助

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

namespace ConsoleApplication1
{
    class Program
    {
        public class Book
        {
            public int UidBook { get; set; }
            public int UidCategory { get; set; }
            public int Score { get; set; }
        }

        public class Category
        {
            public int UidCategory { get; set; }
            public int Score { get; set; }
        }

        public class Author
        {
            public int UidAuthor { get; set; }
            public int Score { get; set; }
        }

        public class AuthorBook
        {
            public int UidAuthor { get; set; }
            public int UidBook { get; set; }
        }

        static void Main(string[] args)
        {
            List<Category> categories = new List<Category>
            {
                new Category() { UidCategory = 1, Score = 25 },
                new Category() { UidCategory = 2, Score = 50 }
            };

            List<Book> books = new List<Book>
            {         
                new Book() { UidBook = 1, UidCategory = 1, Score = 44 },
                new Book() { UidBook = 2, UidCategory = 2, Score = 88 },
                new Book() { UidBook = 3, UidCategory = 1, Score = 99 },
                new Book() { UidBook = 4, UidCategory = 2, Score = 66 }
            };

            List<Author> authors = new List<Author>
            {
                new Author() { UidAuthor = 1, Score = 301 },
                new Author() { UidAuthor = 2, Score = 501 },
                new Author() { UidAuthor = 3, Score = 601 }
            };

            List<AuthorBook> authorBooks = new List<AuthorBook>
            {
                new AuthorBook() { UidAuthor = 1, UidBook = 1 },
                new AuthorBook() { UidAuthor = 1, UidBook = 2 },
                new AuthorBook() { UidAuthor = 2, UidBook = 3 },
                new AuthorBook() { UidAuthor = 3, UidBook = 4 }
            };

            var result = from book in books
                        join category in categories on book.UidCategory equals category.UidCategory
                        join authorBook in authorBooks on book.UidBook equals authorBook.UidBook
                        join author in authors on authorBook.UidAuthor equals author.UidAuthor
                        group new { book, category, authorBook, author } by book.UidBook into groupedResult
                        select new
                        {
                            UidBook = groupedResult.Key,
                            Score   = groupedResult.First().book.Score + 
                                      groupedResult.First().category.Score +
                                      groupedResult.Sum(s => s.author.Score)
                        };

            foreach (var item in result)
            {
                Console.WriteLine("{0} - {1}", item.UidBook, item.Score);
            }

            Console.Read();
        }
    }
}

答案 1 :(得分:0)

我认为你正在寻找这个:

var query = from b in BookList
            join c in CategoryList on b.UidCategory equals c.UidCategory
            join ab in AutorBookList on b.UidBook equals ab.UidBook
            join a in AuthorList on ab.UidAuthor equals a.UidAuthor
            group new { b, c, ab, a} by b.UidBook into g
            let bookScore = g.First().b.Score
            let categoryScore = g.First().c.Score
            let authorScore = g.Sum(x => x.a.Score)
            select new 
            {
              UidBook = g.Key,
              Score = bookScore + categoryScore + authorScore
            };

答案 2 :(得分:0)

var result = from b in bookList
                     join authBook in authorBook on b.UidBook equals authBook.UidBook
                     join auth in authorList on authBook.UidAuthor equals auth.UidAuthor
                     join cat in categoryList on b.UidCategory equals cat.UidCategory
                     select new { b.UidBook, TotalScore = b.Score + auth.Score + cat.Score };

清理,分组,过滤等实际关系。示例1预订多个作者和多个类别,您是否将所有分数相加或取平均值?重量等....