question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can expression in WhenMatched use both existing record and an "insert" dataset?

See original GitHub issue

Hi again Artiom,

I want to add just a little bit sophisticated logic in updating existing record. My expression in WhenMatched compares existing value with a new one and makes a decision:

dataContext.CompanyUsers
                    .Upsert(new CompanyUser(companyDto.Id, userDto.Id, userDto.Position))
                    .On(u => new {u.UserId, u.CompanyId})
                    .WhenMatched(inDb =>
                    new CompanyUser
                    {
                        Positions = inDb.Positions.Contains(userDto.Position) ? inDb.Positions : userDto.Position
                    })
                    .Run();

But this fails with UnsupportedExpressionException.

The following variation of the same fails too:

dataContext.CompanyUsers
                    .Upsert(new CompanyUser(companyDto.Id, userDto.Id, userDto.Position))
                    .On(u => new {u.UserId, u.CompanyId})
                    .WhenMatched((inDb, newOne) =>
                    new CompanyUser
                    {
                        Positions = inDb.Positions.Contains(newOne.Positions) ? inDb.Positions : newOne.Positions
                    })
                    .Run();

Out of curiosity I played a bit with it. But even some simple operations like Positions = inDb.Positions + ','+ userDto.Position fail. Is there any work-around for this? Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dmitriifilonenkocommented, May 24, 2019

Hi Artiom,

Thanks a lot for your efforts! I will do my best to try this ASAP. Though can’t really promise since I’m quite loaded currently with other stuff. But it’s itching to try! 😄

0reactions
artiomchicommented, Aug 11, 2019

I’ll be closing this issue since there’s been no updates in some time. If you have any other issues, please let me know

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upsert into a Delta Lake table using merge
You can upsert data from a source table, view, or DataFrame into a target Delta table by using the MERGE SQL operation.
Read more >
Merging Data Into an Existing Data Source With ON TABLE ...
The ON TABLE MERGE clause in a TABLE or TABLEF command enables you to insert, update, and delete records in an existing single-segment...
Read more >
SQL Server MERGE Statement overview and examples
This article will review on SQL Server MERGE statement, different clauses and examples of MERGE statement in SQL Server.
Read more >
Upsert into a Delta Lake table using merge
You can upsert data from a source table, view, or DataFrame into a target Delta table by using the MERGE SQL operation.
Read more >
SQL MERGE INTO - How to delete then insert matched ...
I am using a MERGE INTO statement to compare these two. When it finds a match between the two tables, it updates the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found