Search code examples
sqlsql-serverlinqlinq-to-sqllinq-to-entities

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 can I do that in LINQ to SQL. Thanks


Solution

  • This was solved. Thanks to 'CodeNotFound' for this answer 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();