我需要帮助将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.对于真实条件.db。Customer.Where(p => p.customer_active(.select(m=>m(.tolist((;1.对于虚假条件.db。Customer.Where(p => !p.customer_active(.select(m=>m(.tolist((;
仅供参考