79652608

Date: 2025-06-04 10:47:43
Score: 0.5
Natty:
Report link
[Sql.Expression(
    @"TRANSLATE({0}, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}|;:''"",.<>/?`~ ', '')",
    ServerSideOnly = true,
    InlineParameters = true)]
public static string ExtractDigits(this string input) 
    => throw new NotImplementedException();

The above way gave me error, that the input string has not right format.

But I managed to make it work like this:

public static class SqlFunctions
{
    [Sql.Expression(
     "ISNULL((" +
     "SELECT STUFF((" +
     "   SELECT '' + SUBSTRING(ISNULL({0}, ''), number, 1) " +
     "   FROM master..spt_values " +
     "   WHERE type = 'P' " +
     "     AND number BETWEEN 1 AND LEN(ISNULL({0}, '')) " +
     "     AND SUBSTRING(ISNULL({0}, ''), number, 1) LIKE '[0-9]' " +
     "   FOR XML PATH('')), 1, 0, '')" +
     "), '')",
     PreferServerSide = true,
     ServerSideOnly = true
    )]
    public static string ExtractNumber(string input) => throw new NotImplementedException();
}

And called this method in my query

public void InsertPayoffData()
{
    using var db = _db();
    
    var query = db.Payoff1C
     .Join(db.Debit,
         p => new { InvoiceNumber = SqlFunctions.ExtractNumber(p.InvoiceNumber), p.InvoiceDate },
         d => new { InvoiceNumber = d.InvoiceNumber.ToString(), d.InvoiceDate },
         (p, d) => new { Payoff = p, Debit = d })
     .Join(db.Kredit,
         pd => new { pd.Payoff.PayDocNumber, pd.Payoff.PayDocDate },
         k => new { k.PayDocNumber, k.PayDocDate },
         (pd, k) => new { pd.Payoff, pd.Debit, Kredit = k })
     .Where(joined => !db.Payoff.Any(pf =>
         pf.Debit_ID == joined.Debit.DebitId &&
         pf.Kredit_ID == joined.Kredit.Kredit_ID))
     .Select(joined => new Payoff
     {
         Debit_ID = joined.Debit.DebitId,
         Kredit_ID = joined.Kredit.Kredit_ID,
         PayoffDate = joined.Payoff.PayOffDate,
         PayoffSum = joined.Payoff.PayOffSum,
         PayoffType = 0
     });

    var result = query.ToList();

    db.BulkInsert(result);
}

Thanks everyone for your help!

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: user19627737