我需要幫助的SQL查詢轉換爲LINQ to SQL的轉換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())
我怎樣才能做到這在LINQ to SQL。謝謝
我需要幫助的SQL查詢轉換爲LINQ to SQL的轉換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())
我怎樣才能做到這在LINQ to SQL。謝謝
這已解決。由於「CodeNotFound」對於這個答案 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();
試試這個代碼
(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();
,如果你需要檢查布爾條件,你應該用這種方式來去除布爾條件和減少代碼
Ef
1.For True Condition db.Customer.Where(p => p.customer_active).select(m => m).tolist(); 1. For False db.Customer.Where(p =>!p.customer_active).select(m => m).tolist();
只是建議
這是一個可愛的有點不可思議,因爲=>'的product_id = product_id'將alwyas返回true。你確定你的SQL有效嗎?無論如何,我們可以擁有實體類和DbContext嗎? – CodeNotFound
是的,這只是一個示例數據,SQL正如您所建議的那樣正常工作。謝謝 –
@CodeNotFound:請查看下面,這是我在我的代碼中做的(這只是一個例子)。如果你想要我標記這個被接受的答案,請添加你的,我會標記它。謝謝 –