Search code examples
c#sqldapper

Splitting tables in C# and SQL


I have got a database which I already implemented in my Web API. I am working at a summer job and I have to use C# that I have never used before. I am not the best programmer but I know how to use java.

I need to split 4 columns each. In my database, there are 4 tables:

  • Name (for the name of a station)
  • Date (for the date the company measured the data)
  • FeedbackType (it is a type of feedback. I got 4 types: Very Negative, Negative, Positive and Very Positive)
  • Count (it describes how many people voted for this specific Feedbacktype).

Every Station has a column for every FeedbackType and my problem is to summarise the feedbacks. Very Negative gives 0 points, Negative gives 1 point, Positive gives 2 points and Very Positive gives 3 points. I have to multiply "Count" with the points given.

Down below you see a little bit of my json file

{
        "Name": "ASFINAG - Parkplatz Radin Nord",
        "Date": "01.07.2019 00:00:00",
        "FeedbackType": "Very Negative",
        "Count": 3
    },
    {
        "Name": "ASFINAG - Parkplatz Radin Nord",
        "Date": "01.07.2019 00:00:00",
        "FeedbackType": "Negative",
        "Count": 1
    },
    {
        "Name": "ASFINAG - Parkplatz Radin Nord",
        "Date": "01.07.2019 00:00:00",
        "FeedbackType": "Positive",
        "Count": 9
    },
    {
        "Name": "ASFINAG - Parkplatz Radin Nord",
        "Date": "01.07.2019 00:00:00",
        "FeedbackType": "Very Positive",
        "Count": 7
    },

This code is just one station just to show you an example of what i have to do

I hope it is not too hard to understand and I really hope you can help me Thank you.


Solution

  • To Feedbacktype by its value and have a total per company:

    We need a way to store Feedbacktype and its respective value. Here we can use a simple array as index will be the value but we could use a Dictionary if the value change in the future

    var feedbackValue = new string[] { "Very Negative", "Negative", "Positive", "Very Positive" };
    

    Finding the multiplicator value for a given feed back will be:

    Array.IndexOf(feedbackValue, x.FeedbackType)       
    

    Then we group on Company name.
    And Sum the count and feedbackValue.

    data.GroupBy(x => x.Name)
        .Select(g => new
        {
            Name = g.Key,
            Total = g.Sum(x =>
                 x.Count * Array.IndexOf(feedbackValue, x.FeedbackType)
                )
        });
    

    Result:

     { Name = Foo, Total = 40 },
     { Name = Bar, Total = 22 }
    

    LiveDemo