EF Core Any in Any客户端评估

时间:2019-06-27 07:29:32

标签: c# entity-framework-core

我有OrderInfo的列表,其中包含Product的ID。 我想加载所有Order(其中包含List<Many2Many>) 我的Product列表中至少有一个IdOrderInfo

基本上它是嵌套的Any Any

下面我作为概念证明编写的代码可以正常工作,但是问题是,当我尝试在EF Core上执行相同操作时,它将在客户端进行评估。

如何使它正确评估?

var list_of_details = new List<OrderInfo> {...};

context
.Orders
.Where(o => o.OrdersProducts.Any(p => list_of_details.Any(d => d.ProductId == p.ProductId)));

public class OrderInfo
{
    public Guid ProductId { get; set; }
    (...)
}

public class Order
{
    public List<Many2Many> OrdersProducts = new List<Many2Many>();
    (...)
}

public class Product
{
    public List<Many2Many> OrdersProducts = new List<Many2Many>();
    (...)
}

public class Many2Many
{
    public Order Order { get; set; }
    public Guid OrderId { get; set; }
    public Product Product { get; set; }
    public Guid ProductId { get; set; }
}

这里是非ef的例子,但是很丑陋,只是作为概念的证明

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

public class Program
{
    public class OrderInfo
    {
        public int ProductId { get; set; }
    }

    public class Order
    {
        public int Id;
        public List<Many2Many> OrdersProducts = new List<Many2Many>();
    }


    public class Product
    {
        public int Id;
        public List<Many2Many> OrdersProducts = new List<Many2Many>();
    }

    public class Many2Many
    {
        public Order Order { get; set; }
        public int OrderId { get; set; }
        public Product Product { get; set; }
        public int ProductId { get; set; }
    }


    public static void Main()
    {
        Console.WriteLine("List of ids that have to be in 'Order' in order to qualify him");
        var list_of_details = Enumerable.Range(0, 5).Select(x => new OrderInfo(){ ProductId = x}).ToList();

        foreach (var item in list_of_details)
        {
            Console.Write(item.ProductId);
        }

        Console.WriteLine();

        // setup
        var orders = new List<Order>();
        var order = new Order(){Id = 2};

        var product = new Product()
        {
            Id = 3,
        };

        order.OrdersProducts.Add(new Many2Many()
        {
            Order = order,
            OrderId = order.Id,
            Product = product,
            ProductId = product.Id
        });

        var order2 = new Order(){Id = 3};
        var product2 = new Product()
        {
            Id = 4,
        };

        order2.OrdersProducts.Add(new Many2Many()
        {
            Order = order2,
            OrderId = order2.Id,
            Product = product2,
            ProductId = product2.Id
        });


        var order3 = new Order(){Id = 1};
        var product3 = new Product()
        {
            Id = 5,
        };

        order3.OrdersProducts.Add(new Many2Many()
        {
            Order = order3,
            OrderId = order3.Id,
            Product = product3,
            ProductId = product3.Id
        });


        orders.Add(order);
        orders.Add(order2);
        orders.Add(order3);
        Console.WriteLine();

        // end setup

        foreach (var ord in orders)
        {
            Console.WriteLine();
            Console.WriteLine($"Order Id: {ord.Id}");
            Console.Write('\t' + "Product Ids: ");
            foreach (var prod in ord.OrdersProducts)
            {
                Console.Write(prod.ProductId);
            }
        }

        Console.WriteLine();
        Console.WriteLine();        
        Console.WriteLine("found orders");
        foreach (var item in orders.Where(o => o.OrdersProducts.Any(p => list_of_details.Any(d => d.ProductId == p.ProductId))))
        {
            Console.WriteLine(item.Id);
        }
    }
}

输出:

List of ids that have to be in 'Order' in order to qualify him
01234


Order Id: 2
   Products Ids: 3
Order Id: 3
   Products Ids: 4
Order Id: 1
   Products Ids: 5

found orders
2
3

1 个答案:

答案 0 :(得分:1)

一种方法是将列表减少为仅id,

packages folder