How to update two columns in linq2db?

Viewed 307

How can I update two column in Linq2db without get. Example: In sql server command

Update teachers 
   set CandidateTotal= (Select count(1) from Candidates where TeacherId = 1)

How can I convert to LINQ2DB query? Do not use SQL commands.

1 Answers

For updating several columns you have to repeat Set operator several times:

db.GetTable<Teacher>()
    .Where(t => t.Id == 1)
    .Set(t => t.CandidateTotal, t => db.GetTable<Candidate>().Where(c => c.TeacherId == t.Id).Count())
    .Set(t => t.IsUpToDate, t => true)
    .Update();

It generates the following SQL:

UPDATE
    [Teacher]
SET
    [CandidateTotal] = (
        SELECT
            Count(*)
        FROM
            [Candidate] [c_1]
        WHERE
            [c_1].[TeacherId] = [Teacher].[Id]
    ),
    [IsUpToDate] = 1
WHERE
    [Teacher].[Id] = 1
Related