var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(products.CategoryId == 1);
return result;
})
.OrderByDescending(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));
var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(products.CategoryId == 1 && products.ModelYear == 2018);
return result;
}).OrderByDescending(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));
var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(products.ListPrice > 300 && products.ModelYear == 2018);
return result;
})
.OrderByDescending(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));
var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(products.ListPrice > 3000 || products.ModelYear == 2018);
return result;
})
.OrderByDescending(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));
Let's start using parameters:
var low = 1899.00M;
var high = 1999.99M;
var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(BETWEEN(products.ListPrice, low, high));
return result;
})
.OrderByDescending(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));
ELINQ maps IList.Contains()
method to SQL IN
operator:
var values = new[] { 299.99M, 369.99M, 489.99M };
var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(values.Contains(products.ListPrice));
return result;
})
.OrderByDescending(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));
And String.Contains()
method to SQL LIKE
operator:
var letters = "Cruiser";
var query = DbContext.Products.Query((Products products) =>
{
var result = SELECT(products);
FROM(products);
WHERE(products.ProductName.Contains(letters));
return result;
})
.OrderBy(p => p.ListPrice);
foreach (var product in query.Take(3))
Console.WriteLine((product.ProductId, product.ProductName, product.CategoryId, product.ModelYear, product.ListPrice));