D
D
Daniil Sidorov2016-12-21 11:24:22
Database
Daniil Sidorov, 2016-12-21 11:24:22

How to make a selection, (many to many)?

Hello, I have a table (many to many) it has fields:

  • id
  • order_id
  • product_id

I want to get a list of products in a specific order.
I tried to get a list of product ids:
var ids = from prd in context.Positions.Where(p => p.order_id == order_id)
            select new
            {
                prd.product_id
            };

Then I tried to find some equivalent of WHERE...IN, but I did not succeed:
var products = from prd in context.Products
                            where ids.Contains(prd.id)
                            select prd;

Until the delivery of the course 8 hours, please help me figure it out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2016-12-21
@ayazer

no need to perform 2 separate requests here, everything can be obtained with one

var products = from p in context.Products 
                               join o in context.Orders on p.Order_Id equals o.Id
                               where o.Id == <НОМЕР ЗАКАЗА>
                               select p;

ADF:
LINQ queries are lazy. This code will just build the query, but won't execute it until the data is actually needed. If you need to transfer them to another context, you can immediately do something like .AsEnumerable() to load all the data into memory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question