how to reference parameters Contain in message body where message body type and values both are in sql table

fahime abouhamze 126 Reputation points
2021-08-15T15:03:09.21+00:00

Hi everybody;
I have a table in witch I save all type of my message frame which I want to send to different person in different level.
table message type is consist of : Id , MessageFrameText that

MessageFrameText : Dear #NameOfReciver#
Hi;
Yesterday Info:
Sales Amount is: #SalesAmt#
Debit : #DebitAmt#
...
and I have an other table in which I save all value : Id , SalesAmt , DebitAmt .....
and also person table and so on .

I want to fill all parameter in message frame with value of other table using lambda query in c# . I should mention it that I use string format successfully but I want to have Parameter like above to be understandable and flexible for every body who see my code. would any one help me ?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,354 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,196 Reputation points
    2021-08-15T16:03:40.843+00:00

    Hello,

    The following is rough but is done so you can see one way to do this. In this case I'm using Entity Framework via LINQ/Lambda.

    The class/project is here

    Code done in a form, could be in a class

    private async void button1_Click(object sender, EventArgs e)
    {
        string Template(CustomerItem item)
        {
            return $"<p>Hello {item.FirstName} {item.LastName}</p>" + 
                   $"<p>We want to validate {item.Phone} is correct.</p>";
        }
        var results = await CustomersTestOperations.GetCustomersAsync();
    
        StringBuilder builder = new StringBuilder();
        foreach (var customerItem in results)
        {
            builder.AppendLine(Template((customerItem)));
            builder.AppendLine("");
        }
    
        Console.WriteLine(builder.ToString());
    
    }
    

    Sample output

    <p>Hello Lúcia Carvalho</p><p>We want to validate (11) 555-1189 is correct.</p>
    
    <p>Hello Anabela Domingues</p><p>We want to validate (11) 555-2167 is correct.</p>
    
    <p>Hello Paula Parente</p><p>We want to validate (14) 555-8122 is correct.</p>
    

    Or here is a 2nd option with the first option still available

    private async void button1_Click(object sender, EventArgs e)
    {
        string Template(CustomerItem item)
        {
            return $"<p>Hello {item.FirstName} {item.LastName}</p>" + 
                   $"<p>We want to validate {item.Phone} is correct.</p>";
        }
    
        string Template1(CustomerItem item)
        {
            return $"<p>Hello #FirstName# #LastName#</p><p>We want to validate #Phone# is correct.</p>"
                       .Replace("#FirstName#", item.FirstName)
                       .Replace("#LastName#", item.LastName)
                       .Replace("#Phone#", item.Phone);
        }
        var results = await CustomersTestOperations.GetCustomersAsync();
    
        StringBuilder builder = new StringBuilder();
        foreach (var customerItem in results)
        {
            builder.AppendLine(Template1((customerItem)));
            builder.AppendLine("");
        }
    
        Console.WriteLine(builder.ToString());
    
    }
    

    Either option I would do in a separate class.


0 additional answers

Sort by: Most helpful