Convert SQL query to LINQ-to-SQL

I need help converting SQL query to LINQ to 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()) 

How to do it in LINQ to SQL. Thanks

0
source share
3 answers

It was agreed. Thanks to "CodeNotFound" for this answer fooobar.com/questions/1267605 / ...

 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(); 
+1
source

try this code

 ( 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
source

you must use this method to remove the boolean condition and reduce the code

if you need to check the bool condition in Ef

1.For True Condition db.Customer.Where (p => p.customer_active) .select (m => m) .tolist (); 1.For the false state db.Customer.Where (p =>! P.customer_active) .select (m => m) .tolist ();

for suggestion only

0
source

All Articles