Try .NET /elinq/
Powered by Try .NET

SQL Server DELETE

Common declarations:

private static void PrepareProductHistory(Products products)
{
    var productHistory = ToTable<Products>(PRODUCT_HISTORY);

    SELECT(products).INTO(productHistory);
    FROM(products);

    Semicolon();
}

1) Delete the number of random rows example

var rows = DbContext.Database.Query((Products products) =>
{
    PrepareProductHistory(products);
    var productHistory = ToTable<Products>(PRODUCT_HISTORY);

    DELETE(TOP(21)).FROM(productHistory);
});

Console.WriteLine($"{rows} rows affected");

2) Delete the percent of random rows example

var rows = DbContext.Database.Query((Products products) =>
{
    PrepareProductHistory(products);
    var productHistory = ToTable<Products>(PRODUCT_HISTORY);

    DELETE(TOP(5).PERCENT()).FROM(productHistory);
});

Console.WriteLine($"{rows} rows affected");

3) Delete some rows with a condition example

var rows = DbContext.Database.Query((Products products) =>
{
    PrepareProductHistory(products);
    var productHistory = ToTable<Products>(PRODUCT_HISTORY);

    DELETE().FROM(productHistory);
    WHERE(productHistory.ModelYear == 2017);
});

Console.WriteLine($"{rows} rows affected");

4) Delete all rows from a table example

var rows = DbContext.Database.Query((Products products) =>
{
    PrepareProductHistory(products);
    var productHistory = ToTable<Products>(PRODUCT_HISTORY);

    DELETE().FROM(productHistory);
});

Console.WriteLine($"{rows} rows affected");

< BACK | HOME