2017-05-08 2 views
0

내가 그 LINQ에서 SQL로 할 수있는 방법는 LINQ - 투 - SQL

select top 5 customer_id, customer_name, product_id 
from Customer 
Join Product on product_id = product_id 
where (customer_active = 'TRUE') 
order by checksum(newid()) 

을 SQL로 LINQ에 SQL 쿼리를 변환하는 데 도움이 필요로 SQL 쿼리를 변환합니다. 감사합니다

+1

에 대한

이 때문에 => 'PRODUCT_ID = product_id' alwyas true를 돌려줍니다 릴 비트 이상이다. SQL이 제대로 작동합니까? 어쨌든 엔티티 클래스와 DbContext를 가질 수 있습니까? – CodeNotFound

+0

네, 이건 그냥 예제 데이터입니다, SQL은 당신이 제안한 것처럼 잘 작동합니다. 감사합니다 –

+0

@CodeNotFound : 아래에서 확인하십시오, 이것은 어떻게 내 코드에서 한 것입니다 (이것은 단지 예입니다). 내가이 허용 된 대답을 표시하게하려면, 당신을 추가하십시오. 나는 그것을 표시 할 것입니다. 감사합니다 –

답변

1

이것은 해결되었습니다. 이 답변 https://stackoverflow.com/a/43850748/1655774

db.Customer.Where(p => p.customer_active == true).Select(p => new CustomerViewModel 
    { 
     Customer_id= p.customer_id, 
     Customer_name = p.customer_name, 
     Product_id = p.Product.product_id 
    }).OrderBy(c => SqlFunctions.Checksum(Guid.NewGuid())).Take(5).ToList(); 
0

당신은 부울 상태를 확인해야하는 경우 코드를

을 부울 조건을 제거하고 줄이기 위해이 방법을 사용한다

(from p in Customer 
     join q in Product on p.product_id equals q.product_id 
     join q in Product on p.product_id equals q.product_id 
     where customer_active ==true select new 
     { 
      customer_id=p.customer_id, 
      customer_name=p.customer_name, 
      product_id=q.product_id 
     }).OrderBy(c => SqlFunctions.Checksum(Guid.NewGuid())).Take(5).ToList(); 
0

이 코드를 시도위한 'CodeNotFound'덕분에 in Ef

1. 사실 조건 db.Customer.Where (p => p.customer_active) .select (m => m) .tolist(); 1. 거짓 조건 용 db.Customer.Where (p =>! p.customer_active) .select (m => m) .tolist(); 단지 제안