DEV Community

loading...
Cover image for Linq2DB CRUD Operations

Linq2DB CRUD Operations

rajpawar profile image Raj ・2 min read

linq2db is a .NET Foundation project.

Get more information regarding Linq2DB here

Here I have listed some methods for basic CRUD operations using linq2db.

Note:- This information is not for beginners as here chunks of code are present not complete crud operations are performed.

CreateTable and DropTable

using (var db = new DataConnection())
{
    try { db.DropTable<TestTable>(); } catch {}

    db.CreateTable<TestTable>();
}
Enter fullscreen mode Exit fullscreen mode

Insert

This method inserts a single object.

using (var db = new DataConnection())
{
    db.Insert(new TestTable
    {
        Name = "Crazy Frog",
    });
}
Enter fullscreen mode Exit fullscreen mode

This method takes Expression Tree and converts it to SQL

using (var db = new DataConnection())
{
    db.GetTable<TestTable>()
        .Insert(() => new TestTable
        {
            Name      = "Crazy Frog",
            CreatedOn = Sql.CurrentTimestamp
        });
}
Enter fullscreen mode Exit fullscreen mode

This method generates INSERT INTO / SELECT statement

using (var db = new DataConnection())
{
    db
        .GetTable<TestTable>()
        .Where(t => t.Name == "Crazy Frog")
        .Insert(
            db.GetTable<TestTable2>(),
            t => new TestTable2
            {
                Name      = t.Name + " II",
                CreatedOn = t.CreatedOn.Value.AddDays(1)
            });
}
Enter fullscreen mode Exit fullscreen mode

Into, Value, Insert

Alternative for those who don't like new operator for Insert.

using (var db = new DataConnection())
{
    db
        .GetTable<TestTable>()
        .Where(t => t.Name == "Crazy Frog")
        .Into(db.GetTable<TestTable2>())
            .Value(t => t.Name,      t => t.Name + " II")
            .Value(t => t.CreatedOn, t => t.CreatedOn.Value.AddDays(1))
        .Insert();
}
Enter fullscreen mode Exit fullscreen mode

InsertWithIdentity

This method inserts a row and returns identity value

using (var db = new DataConnection())
{
    object identity = db.GetTable<TestTable>()
        .InsertWithIdentity(() => new TestTable
        {
            Name      = "Crazy Frog",
            CreatedOn = Sql.CurrentTimestamp
        });
}
Enter fullscreen mode Exit fullscreen mode

InsertOrUpdate

This method updates an existing record or inserts a new one if it does not exist.

using (var db = new DataConnection())
{
    db.GetTable<TestTable3>()
        .InsertOrUpdate(
            () => new TestTable3
            {
                ID   = 5,
                Name = "Crazy Frog",
            },
            t => new TestTable3
            {
                Name = "Crazy Frog IV",
            });
}
Enter fullscreen mode Exit fullscreen mode

InsertOrReplace

This method updates all columns of an existing record or inserts new one if it does not exist

using (var db = new DataConnection())
{
    db.InsertOrReplace(
        new TestTable3
        {
            ID   = 5,
            Name = "Crazy Frog",
        });
}
Enter fullscreen mode Exit fullscreen mode

Update

This method updates all columns of existing record

using (var db = new DataConnection())
{
    db.Update(
        new TestTable3
        {
            ID   = 5,
            Name = "Crazy Frog",
        });
}
Enter fullscreen mode Exit fullscreen mode

This method updates only columns you specify

using (var db = new DataConnection())
{
    db
        .GetTable<TestTable>()
        .Where(t => t.ID == 1)
        .Update(t => new TestTable
        {
            Name = "Crazy Frog",
        });
}
Enter fullscreen mode Exit fullscreen mode

Overload with WHERE clause

using (var db = new DataConnection())
{
    db
        .GetTable<TestTable>()
        .Update(
            t => t.ID == 1,
            t => new TestTable
            {
                Name = "Crazy Frog",
            });
}
Enter fullscreen mode Exit fullscreen mode

An alternative for those who don't like new operator for Update

using (var db = new DataConnection())
{
    db
        .GetTable<TestTable>()
        .Where(t => t.ID == 1)
        .Set(t => t.Name,      t => "Crazy Frog IV")
        .Set(t => t.CreatedOn, t => t.CreatedOn.Value.AddHours(1))
        .Update();
}
Enter fullscreen mode Exit fullscreen mode

Delete

This method deletes an existing record

using (var db = new DataConnection())
{
    db
        .GetTable<TestTable>()
        .Where(t => t.ID == 1)
        .Delete();
}
Enter fullscreen mode Exit fullscreen mode

Find more information on 3rd party Linq2db documentation here

Discussion (0)

pic
Editor guide