EF Core:LINQ选择“多对多”到“多对多”

问题描述 投票:4回答:2

我有一个表“参考”和一个表“文章”,其中一篇文章引用其他文章。

我有简单的引用,如:A - > B.

SQL:

select ab.*
from Article a
inner join Reference ab on ab.ArticleFromId = a.Id
inner join Article b on b.Id = ab.ArticleToId
where a.ArticleNo = "1234"

C#LINQ:

_context.Reference
   .Where(r => r.ArticleFromNavigation.ArticleNo.Equals("1234"));

我也有参考链,如:A - > B - > C(让我们假设链中最多只有3篇文章)

SQL:

select ab.ArticleFromId, bc.ArticleToId
from Article a
inner join Reference ab on ab.ArticleFromId = a.Id
inner join Article b on b.Id = ab.ArticleToId
inner join Reference bc on bc.ArticleFromId = b.Id
inner join Article c on c.Id = bc.ArticleToId
where a.ArticleNo = "1234"

这在SQL中很容易,因为结果只是与其他连接相乘,但我不知道如何在LINQ中编写它。

我希望它是这样的(它不会工作):

_context.Reference
   .Where(r => r.ArticleFromNavigation.ArticleNo.Equals("1234"))
   .Select(r => new Reference
   {
       ArticleFromNavigation = r.ArticleFromNavigation, //this is article "A"
       ArticleToNavigation = r.ArticleToNavigation.ReferenceArticleToNavigations //this wont work as it's a iCollection
   }).AsNoTrackable();

在这里,我想要“A - > C”类型“参考”的新结果。我想我必须在“新参考”部分之前包含/ theninclude / join / select / selectmany(?)集合,但我不知道。

有什么方法可以存档吗?

c# sql linq .net-core ef-core-2.1
2个回答
3
投票

好吧,你可以像在SQL中一样完成它,但使用导航属性而不是连接。

我将使用LINQ查询语法,因为它更好地显示了相似性,并且方法语法也非常复杂,并且难以阅读此类查询:

from a in _context.Article
from ab in a.ReferenceArticleFromNavigations
let b = ab.ArticleToNavigation
from bc in b.ReferenceArticleFromNavigations
let c = bc.ArticleToNavigation
where a.ArticleNo = "1234"
select new Reference
{
    ArticleFromNavigation = a,
    ArticleToNavigation = c,
}

不强烈需要let语句(您可以直接使用引用导航属性),我只是为了使LINQ查询更接近SQL查询而包含它们。

实际上,在这种情况下等效的方法并不坏 - 用嵌套的SelectMany展平几个级别,并使用SelectMany重载投影(顶部,底部)对,允许:

_context.Article
    .Where(a => a.ArticleNo = "1234")
    .SelectMany(a => a.ReferenceArticleFromNavigations
        .SelectMany(ab => ab.ArticleToNavigation.ReferenceArticleFromNavigations)
        // include as many `SelectMany` like the above as you wish until you hit the desired level of nesting
        .Select(bc => bc.ArticleToNavigation),
    (a, c) => new Reference
    {
        ArticleFromNavigation = a,
        ArticleToNavigation = c,
    });

1
投票

我将数据库建模为类以使语法正确。见下面的代码:

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

namespace ConsoleApplication107
{
    class Program
    {
        static void Main(string[] args)
        {
            Context _context = new Context();
            string ArticleNo = "1234";

            var results = (from a in _context.article.Where(x => x.Id == ArticleNo)
                           join ab in _context.reference
                              .Where(x => (x.ArticleFromId == x.ArticleToId))
                              on a.Id equals ab.ArticleFromId
                           select new { a = a, ab = ab }
                          ).Select(r => new Reference()
                          {
                              ArticleFromNavigation = r.a,
                              ArticleToNavigation = r.a.ReferenceArticleToNavigations.ToList() 
                          }).ToList();
        }
    }

    public class Context
    {
        public List<Reference> reference { get; set; }
        public List<Article> article { get; set; }
    }
    public class Reference
    {
        public string ArticleFromId { get; set; }
        public string ArticleToId { get; set; }
        public Article ArticleFromNavigation { get; set; }
        public List<string> ArticleToNavigation { get; set; }
    }
    public class Article
    {
        public string Id { get; set; }
        public List<string> ReferenceArticleToNavigations { get; set; }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.