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.
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
Related
I am currently in the process of building a .NET Web API which is gonna be consumed in another project for a dashboard. Currently, first endpoint return total number of parcels and an array of parcels sent for a given date range.
An example of this API endpoint response looks something like this
{
"totalCount": 3,
"data": [
{
"parcelId": 1,
"parcelSentDate": "2022-04-25",
"parcelCount": 1
},
{
"parcelId": 2,
"parcelSentDate": "2022-04-26",
"parcelCount": 1
},
{
"parcelId": 3,
"parcelSentDate": "2022-04-27",
"parcelCount": 1
}
],
"success": true
}
Second endpoint returns a total number of parcels on whether they have been delivered either early, late or on time and has an array of parcels which contains the delivered date and the estimated delivery date.
An example of this API endpoint response looks like the below
{
"early": 1,
"late": 2,
"onTime": 0,
"data": [
{
"parcelId": 1,
"deliveredDate": "2022-05-04",
"estimatedDeliveryDate": "2022-05-02",
},
{
"parcelId": 2,
"deliveredDate": "2022-05-01",
"estimatedDeliveryDate": "2022-04-30",
},
{
"parcelId": 3,
"deliveredDate": "2021-05-26",
"estimatedDeliveryDate": "2022-05-09",
}
],
"success": true
}
The total number of parcels sent can be calculated using the second endpoint from the parcels array. What I am wondering about is, is it still okay to keep the first endpoint that returns the total number of parcels sent and keep things separated? or is it better to keep only the second endpoint and derive the total parcels count from it since it will contain all the parcels information?
Does it all come down to the situation and there isn't a right or wrong answer here?
You can keep only one endpoint but separate with any specific parameters like early, late or on time that depends on your business requirements. If these parameters passed then return the second endpoint functionality or just return the first endpoint functionality. If these parameters are not required put just one parameter like with_specifications = true and then return second endpoint functionality otherwise if it false return the first endpoint functionality.
List<int[,]> table = new List<int[,]> { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
This isn't right, but I'm stumped on how to do it.
Basically in the end I want to say table[0] and get back the first tuple, table[2] and get back the second tuple, and so on.
What is the correct format (if I have the right data model) or the correct data structure for what I want to do?
I have a pretty hefty list of tuples in the above format, And I like to stay with the format if at all possible.
I need help with expression for sorting both ascending and descending for the same data.
I can have multiple expression separated by pipe but i can't seem to find any example that does both ASC and DESC in the same call.
So what i am looking for is equal to following sql query.
select *
from dummyData
order by id, code desc, update_only
Here is sample.
[
{
"id": 1,
"code": "y",
"update_only": 0.0
},
{
"id": 2,
"code": "a",
"update_only": 0.0
},
{
"id": 1,
"code": "z",
"update_only": 0.0
},
{
"id": 2,
"code": "b",
"update_only": 0.0
}
]
I can do following for the ordering
sort_by(array elements, expression->number|expression->string expr)
How can i do the following with one call rather than the two calls that i have?
sort_by(myarray, &id | &update_only)[];
reverse(sort_by(myarray, &code))[];
Or by doing multiple calls, don't want to do this as well.
result1 = sort_by(myarray, &id)[];
result2 = reverse(sort_by(result1, &code))[];
resilt3 = sort_by(myarray, &update_only)[];
Your actual expression is incorrect, you will realise, if you change a value of update_only that &id | &update_only is not a thing.
What is described in the documentation:
expression->number|expression->string expr
Is actually meaning that the expression can either be of type string or of type number, not that you can chain multiple sorting using the pipe sign.
An explanation of this is made here:
JMESPath has various built-in functions that operate on different data types, documented below. Each function below has a signature that defines the expected types of the input and the type of the returned output:
return_type function_name(type $argname)
return_type function_name2(type1|type2 $argname)
The list of data types supported by a function are:
number (integers and double-precision floating-point format in JSON)
string
boolean (true or false)
array (an ordered, sequence of values)
object (an unordered collection of key value pairs)
null
expression (denoted by &expression)
With the exception of the last item, all of the above types correspond to the types provided by JSON.
If a function can accept multiple types for an input value, then the multiple types are separated with |. If the resolved arguments do not match the types specified in the signature, an invalid-type error occurs.
Source: https://jmespath.org/specification.html#built-in-functions, emphasis, mine
Now, what want to achieve is pretty simple.
When you are sorting on multiple columns, it is like chaining the sorts, from the lower priority to the upper priority.
So that means that you can do (in pseudo code):
sort by id (
reverse (
sort by code (
reverse (
// ^--- this is the trick, so when you will reverse it
// again, to have the DESC sorting on `code`, you'll end
// up with the sorting of `update_only` in the "correct" order
sort by update_only
)
)
)
)
So, translated in a JEMSPath expression:
sort_by(reverse(sort_by(reverse(sort_by(#,&update_only)),&code)),&id)
This expression on the following JSON (added an extra case from your example to showcase the update_only sorting:
[
{
"id": 1,
"code": "y",
"update_only": 0.0
},
{
"id": 2,
"code": "a",
"update_only": 0.0
},
{
"id": 1,
"code": "z",
"update_only": 0.0
},
{
"id": 2,
"code": "b",
"update_only": 1.0
},
{
"id": 2,
"code": "b",
"update_only": 0.0
}
]
Will give:
[
{
"id": 1,
"code": "z",
"update_only": 0
},
{
"id": 1,
"code": "y",
"update_only": 0
},
{
"id": 2,
"code": "b",
"update_only": 0
},
{
"id": 2,
"code": "b",
"update_only": 1
},
{
"id": 2,
"code": "a",
"update_only": 0
}
]
I have a web application where each user also has GPS coordinates (SqlGeography). As of now, I calculate the distance between two GPS coordinates as Euclidean distance with the Distance function.
Some users have requested that I replace the approximated distance (in terms of air-line distance) with a more realistic distance. I'm thinking about using specific routing APIs (e.g., from Google, Here or Azure) to get a more realistic distance, for instance for a car ride.
Currently, I have approximately 5000 different GPS coordinates and I would like to calculate and store a pair-wise distance for all of these pairs. This would result in 5000 * 5000 queries / requests which is very expensive. Is there any way how I can reduce the quadratic complexity with a trick I haven't thought of?
Some APIs have an option to add waypoints/stops between start and end of a route( i.e. Sygic web API.
Maybe Google and Co. do something similar.
Response includes legs property, representing route/duration from start to waypoint1, wapoint1 to waypointN, waypointN to end of route ... etc.
{
"routes": [
{
"route": "oupjIa}w|#^ErCUPELEf##`A[nAjGlBnFfBtERl#~DdNdA~DdBjG|BhKfArELbAd#`EJdFKxF?rBVfKF~BFjH#jEChJC`DMtLCzEApB#bIRdOZlGdAtLr#tErAxGnIj\\fBnIrBxMr#|Gh#fHf#jJPvFDvAHxBBf#X|EA`Br#zLd#|Dz#hF`AzElBhIvClMnA|Ib#dGd#bKb#rYBbBJ|CDxGFjEx#tm#N`ID|CF`PStMI|B[`Hi#vIcA`KkC|PcBtIoBlIaF`RgN~c#oGpTqGrWaFtWyEd\\mAlKcBpQkBbX{#~PcAlYG`CClAEdBAj#KdEe#z`#AlBCjDC|XJ~WTrTh#rWZ`Pf#tUTfK`#fQ|#d]v#|O|BpYhBpOrE`\\bC`RDZf#fEvBlULtBZfFJhBNxCBZn#xQ\\~TZb`#JxQ#jNO~PeAfa#mDzdAo#jOsArSmDx_#eAnK}AlRs#zNg#rRChUB|ENzKZnR~B~tAvBthAv#nUtAdWbCl\\p[zbDrFhh#nDvZdB`Mb#~C|#xGrJ~n#fIzb#pBlJv#lDzAxGrDtOzEnQ|Rdr#|FzU|ArH|DfU~ArLx#xG^dDtBbQb#nDhAtIFf#\\`CZvBDXbAbHDXbBvKv#`E|BfJd#~BVlAJh#tA~Gb#tB\\~AtDbPrB~IxB~IzB`JvCdLzAdGjAvELb#Nn#|#xDR~#r#jDzAjI`#hCT~AhDbVF\\fAjIlBbOrAlKjAtIr#lFz#nG#JJv#v#`G`#|Cd#jDDhAJp#D^TzB#ZBpCStEYfB_#pAeAlCINo#~#oBbBoCrAaFfB_H`BsGhA}Ch#gMrB}Bd#oAR_APyOlCwAV}EdASF]JcErA_BXe#G]w#EsBNu#l#yBZe#j#]f#GjBTx#JXF#kApAc]ImEO_AWq#uAkAgAe#eCyA_CoDm#s#cA[eCM?mIYiBsAcEy#uBcA}A]MeAGu#?e#VsEVw#Ho#JiBHwAEa#SaAKy#Cs#PuAlAc#r#sChE{CjFkA`D}AbGIj#y#bEW`Ai#rAw#tA",
"eta": 1531834797,
"duration": {
"value": 1713,
"text": "28 minutes 33 seconds"
},
"distance": {
"value": 36430,
"text": "36.43 km"
},
"legs": [
{
"distance": {
"value": 36430,
"text": "36.43 km"
},
"duration": {
"value": 1713,
"text": "28 minutes 33 seconds"
},
"start_location": {
"latitude": 54.32168,
"longitude": 10.12193
},
"end_location": {
"latitude": 54.30731,
"longitude": 9.66195
},
"route": "oupjIa}w|#^ErCUPELEf##`A[nAjGlBnFfBtERl#~DdNdA~DdBjG|BhKfArELbAd#`EJdFKxF?rBVfKF~BFjH#jEChJC`DMtLCzEApB#bIRdOZlGdAtLr#tErAxGnIj\\fBnIrBxMr#|Gh#fHf#jJPvFDvAHxBBf#X|EA`Br#zLd#|Dz#hF`AzElBhIvClMnA|Ib#dGd#bKb#rYBbBJ|CDxGFjEx#tm#N`ID|CF`PStMI|B[`Hi#vIcA`KkC|PcBtIoBlIaF`RgN~c#oGpTqGrWaFtWyEd\\mAlKcBpQkBbX{#~PcAlYG`CClAEdBAj#KdEe#z`#AlBCjDC|XJ~WTrTh#rWZ`Pf#tUTfK`#fQ|#d]v#|O|BpYhBpOrE`\\bC`RDZf#fEvBlULtBZfFJhBNxCBZn#xQ\\~TZb`#JxQ#jNO~PeAfa#mDzdAo#jOsArSmDx_#eAnK}AlRs#zNg#rRChUB|ENzKZnR~B~tAvBthAv#nUtAdWbCl\\p[zbDrFhh#nDvZdB`Mb#~C|#xGrJ~n#fIzb#pBlJv#lDzAxGrDtOzEnQ|Rdr#|FzU|ArH|DfU~ArLx#xG^dDtBbQb#nDhAtIFf#\\`CZvBDXbAbHDXbBvKv#`E|BfJd#~BVlAJh#tA~Gb#tB\\~AtDbPrB~IxB~IzB`JvCdLzAdGjAvELb#Nn#|#xDR~#r#jDzAjI`#hCT~AhDbVF\\fAjIlBbOrAlKjAtIr#lFz#nG#JJv#v#`G`#|Cd#jDDhAJp#D^TzB#ZBpCStEYfB_#pAeAlCINo#~#oBbBoCrAaFfB_H`BsGhA}Ch#gMrB}Bd#oAR_APyOlCwAV}EdASF]JcErA_BXe#G]w#EsBNu#l#yBZe#j#]f#GjBTx#JXF#kApAc]ImEO_AWq#uAkAgAe#eCyA_CoDm#s#cA[eCM?mIYiBsAcEy#uBcA}A]MeAGu#?e#VsEVw#Ho#JiBHwAEa#SaAKy#Cs#PuAlAc#r#sChE{CjFkA`D}AbGIj#y#bEW`Ai#rAw#tA",
"eta": 1531834797
}
]
}
],
"status": "OK",
"copyright": "© 2018 Sygic a.s."
}
Ok, I get the title sounds weird, but here's my scenario. I think ts a simple query, but its hard to explain on paper. And I'm having trouble with the LINQ myself. Best to explain by example:
I have a list of objects (in psuedocode) that looks like:
var data = [
{
quantity: 123,
rate: 0.12
},
{
quantity: 456,
rate: 0.13
},
{
quantity: 3,
rate: 0.14
},
{
quantity: 92,
rate: 0.15
}
]
I am trying to generate a LINQ query that returns rate equaling the total sum of an input quantity.
Explaination by example:
Using the example data above, I want to find the rate that matches a total quantity of 581 (a variable input from elsewhere). The first three objects in my list have quantities that total 582, so I want my LINQ query to return 0.14 (the rate matching the condition that the total quantity fields is greater than my input quantity).
I should be able to use LINQ to do this, right? Its sort of a .Sum and a .Where, and that's where I'm getting confused.
Are there any references out that to address this type of scenario? Does anyone have any experience writing a "conditional sum"?
In functional programming, this would be a variation of a reduce. In LINQ, the Aggregate method can be used for this purpose. Psuedo-LINQ:
var limit = 581;
var rate = data.Aggregate((acc, cur) =>
{
if (acc.quantity < limit)
{
return new
{
quantity = acc.quantity + cur.quantity,
rate = cur.rate
};
}
else
{
return acc;
}
}).rate;
EDIT: A more compact, one-line version:
var rate = data.Aggregate((acc,cur) => acc.quantity < limit ? new { quantity = acc.quantity + cur.quantity, rate = cur.rate } : acc).rate;
Was going for Aggregate but failed to get in one line :(
For the desired of one-liner, this is a funny usage of LINQ :)
var target = 581;
var sum = 0;
var rate = data.First(i =>(sum += i.Quantity) > target).Rate; // 0.14