TimeScaleData on C# - c#

I am migrating a macro code into VSTO. I am having problems with TimeScaleData. the macro code is like this.
For Each tsk In ActiveProject.Tasks
If tsk.Summary = False And tsk.Duration <> 0 Then
With tsk
Set tsvs = .TimeScaleData(StartDate:=ActiveProject.ProjectStart, EndDate:=ActiveProject.ProjectFinish, Type:=pjTaskTimescaledBaselineWork, timescaleunit:=pjTimescaleWeeks, Count:=1)
End With
For Each tsv In tsvs
differencedate = 0
If tsk.BaselineStart <> "NA" And tsk.BaselineFinish <> "NA" Then
If tsk.BaselineStart >= tsv.StartDate And tsk.BaselineStart <= tsv.StartDate + 7 And tsk.BaselineFinish <= tsv.StartDate + 7 Then
differencedate = Application.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ActiveProject.Calendar) / 60
ElseIf tsk.BaselineStart >= tsv.StartDate And tsk.BaselineStart <= tsv.StartDate + 7 And tsk.BaselineFinish >= tsv.StartDate + 7 Then
differencedate = (Application.DateDifference(tsk.BaselineStart, tsv.StartDate + 7, ActiveProject.Calendar)) / 60
ElseIf tsk.BaselineStart <= tsv.StartDate And tsk.BaselineFinish >= tsv.StartDate And tsk.BaselineFinish <= tsv.StartDate + 7 Then
differencedate = (Application.DateDifference(tsv.StartDate, tsk.BaselineFinish, ActiveProject.Calendar)) / 60
ElseIf tsk.BaselineStart <= tsv.StartDate And tsk.BaselineFinish >= tsv.StartDate + 7 Then
differencedate = Application.DateDifference(tsv.StartDate, tsv.StartDate + 7, ActiveProject.Calendar) / 60
End If
End If
Next
End if
Next
I need to translate this to C#, anyone who can help?

What code do you have so far? Any exceptions in the code?
Anyway, you can use automatic code converters to get started quickly. Then you can correct the code manually.
Take a look at the sample code listed in the Microsoft Project: TimeScaleData Method from .NET without GC.Collect article.

Related

What regular expression need for match nested expression?

I'm triying to make a regex in .net to match nested expresion.
I wan't to extract a condition and value1 and value2 like this:
CASE WHEN condition THEN value1 ELSE value2 END
The string that i would like to match it's this:
CASE WHEN 5 + CASE WHEN 6 < 3 THEN 5 ELSE 2 END = 0 THEN 0 ELSE CASE WHEN 6 < 0 THEN 2 ELSE 1 END / (3 + CASE WHEN 5 < 0 THEN 2 ELSE 0 END) END
Formatted string:
CASE WHEN 5 +
CASE WHEN 6 < 3
THEN 5
ELSE 2
END = 0
THEN 0
ELSE
CASE WHEN 6 < 0
THEN 2
ELSE 1
END / (3 + CASE WHEN 5 < 0
THEN 2
ELSE 0
END)
END
I use a below Regex string, but only match without nested expresion:
(CASE WHEN )(?<cond>[\s\S]*?)( THEN )(?<val1>[\s\S]*?)( ELSE )(?<val2>[\s\S]*?)( END)
What regular expression need to extract the first "CASE WHEN" as below?
Condition: 5 + CASE WHEN 6 < 3 THEN 5 ELSE 2 END = 0
Value1: 0
Value2: CASE WHEN 6 < 0 THEN 2 ELSE 1 END / (3 + CASE WHEN 5 < 0 THEN 2 ELSE 0 END)

Select Specific Coordinates From List

I have a list of coordinates (x, y) ordered from left to right, top to bottom:
My goal is to grab only some of them in a different order. I've numbered the only points I want to grab and in the order I want to grab them. Similar to Snake. The direction reverses each time I move down one:
I've attempted writing out the logic for how to select each point, but I can't seem to work out how to get it in a for loop. Or a nested for loop, I think I'll need.
length = 10 (the size of the square of coordinates I'm grabbing. Which is 10x10.)
Increments * +
1) length * 0 + 0 // +0 +0
2) length * 1 + 0 // +1 +0
3) length * 2 + 0 // +1 +0
4) length * 1 + 1 // -1 +1
5) length * 2 + 1 // +1 +0
6) length * 3 + 0 // +1 -1
7) length * 4 + 0 // +1 +0
8) length * 3 + 1 // -1 +1
9) length * 4 + 1 // +1 +0
10) length * 5 + 0 // +1 -1
So if I were to hard code the array for each coordinate I want, I would have:
1) 0
2) 10
3) 20
4) 11
5) 21
6) 30
7) 40
8) 31
9) 41
10) 50
I do not think this requires a nested loop.
You can use a flip variable to change whether or not a specific line is "flipped".
Each row has two indices to be taken
flip alternates between 0 and 1, every two indices (i.e. every row)
On even rows, the first index is length - 1 less than the second index
On odd rows, that relationship is flipped.
public static void Main(string[] args)
{
int length = 11;
for (int i = 0; i < 10; ++ i) {
int flip = (i / 2) % 2;
int index = (i / 2) * length + ((i + flip) % 2) * (length - 1);
Console.WriteLine(index);
}
}
Output:
0
10
11
21
22
32
33
43
44
54

Get sum of time column in sql server

What I tried is:
select cast(sum(datediff(second,0,totalhr))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,totalhr))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,totalhr))%60 as varchar(2)),2) as total
FROM checkinout where YEAR(date)=2019 and MONTH(date)=09 and userid=5
It giving wrong output 112:53:04 the right answer should be 116:30:04
If the totalhr column type is time and the format is hh:mm:ss,
For SQL Server: DATEADD (datepart , number , date )
SELECT DATEADD (ms, SUM (DATEDIFF (ms, '00:00:00.000', totalhr)), '00:00:00.000') AS total
FROM checkinout
WHERE
YEAR(date) = 2019 AND
MONTH(date) = 9 AND
userid = 5;
This function adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that modified value.
the above condition will return the calculated Date and Time but if you want just the hh:mm:ss in two digit format you can use:
SELECT FORMAT(hrs, '0#') + ':' + FORMAT(mins, '0#') + ':' + FORMAT(secs, '0#') as total FROM
(SELECT hrs + (((((mins * 60) + (secs - (secs % 60))) / 60) - ((((mins * 60) + (secs - (secs % 60))) / 60) % 60)) / 60) AS hrs,
(((mins * 60) + (secs - (secs % 60))) / 60) % 60 AS mins,
secs % 60 AS secs
FROM (
SELECT SUM(116) AS hrs, // you have to replace the number with your column
SUM(30) AS mins, // you have to replace the number with your column
SUM(04) AS secs // you have to replace the number with your column
) AS dateSplit) AS total
try running below query
SELECT cast(sum(DATEPART(HOUR,totalhr)) as varchar(max)) + ':' +
cast(sum(DATEPART(MINUTE,totalhr)) as varchar(max)) + ':' +
cast(sum(DATEPART(SECOND,totalhr)) as varchar(max)) FROM checkinout

Get range in multiplication of 5

I have a number. For instance, my number is 19 . Then I want to populate my drop down with range in multiplication of 5. So my dropdownlist will consist of items of:
1-5
6-10
11-15
16-19
I tried modulus and division, however, I can't seems to get the range. Is there a fixed method?
Sample code
List<string> range = new List<string>();
int number = 19;
int numOfOccur = (19/5);
for (int i = 1; i < numOfOccur ; i++)
{
range.Add(i + " - " + (i * 5))
}
Sometime I think that old school code, without fancy linq is a bit more clear
int maximum = 19;
int multiple = 5;
int init = 1;
while (init + multiple <= maximum )
{
string addToDDL = init.ToString() + "-" + (init + multiple - 1).ToString();
Console.WriteLine(addToDDL);
init += multiple;
}
if(init <= maximum)
{
string last = init.ToString() + "-" + maximum.ToString();
Console.WriteLine(last);
}
Linq solution (modern techs allow us to put it consize):
int number = 19;
int div = 5;
List<string> range = Enumerable
.Range(0, number / div + (number % div == 0 ? 0 : 1))
.Select(i => $"{i * div + 1} - {Math.Min((i + 1) * div, number)}")
.ToList();
Test
Console.Write(string.Join(Environment.NewLine, range));
Returns
1 - 5
6 - 10
11 - 15
16 - 19
When using modulo arithmetics, do not forget about remainders: you have an error in int numOfOccur = (19/5); line. It should be
int numOfOccur = 19 / 5 + (19 % 5 == 0 ? 0 : 1);
for the last incomplete 16 - 19 range to be proceeded.
Add this package to your project : https://www.nuget.org/packages/System.Interactive/
Then you can do this:
IEnumerable<IList<int>> buffers2 = Enumerable.Range(1, 19).Buffer(5);
IList<int>[] result2 = buffers2.ToArray();
// { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, ...
Don't forget to add System.Interactive namespace to your using block.

Wilson Score for 1-5 star rating in C#

I found this really well explained calculation but I really don't know if it's correct or not. I've seen a lot of posts on the subject I havent seen one calculate for 5
the most important part is this:
Stars Negative Positive Total
0 0 0 0
1 1 0 1
2 0.75 0.25 1
3 0.5 0.5 1
4 0.25 0.75 1
5 0 1 1
SET new.total = new.positive + new.negative,
new.stars = ROUND( (((new.positive / new.total) * 4) + 1) * 2, 0) / 2,
new.lower_bound = ((new.positive + 1.9208) / (new.positive + new.negative) - 1.96 * SQRT((new.positive * new.negative) / (new.positive + new.negative) + 0.9604) / (new.positive + new.negative)) / (1 + 3.8416 / (new.positive + new.negative))
which I converted to C# as the following
double posf = model.stars == 1 ? 0 :
model.stars == 2 ? 0.25 :
model.stars == 3 ? 0.5 :
model.stars == 4 ? 0.75 :
model.stars == 5 ? 1 : 0;
double negf = model.stars == 1 ? 1 :
model.stars == 2 ? 0.75 :
model.stars == 3 ? 0.5 :
model.stars == 4 ? 0.25 :
model.stars == 5 ? 0 : 0;
rating.positif += posf;
rating.negatif += negf;
rating.ratingcount += 1;
rating.ratingavg = Math.Round((((rating.positif / rating.ratingcount) * 4) + 1) * 2, 0) / 2;
rating.calcSort = ((rating.positif + 1.9208) / (rating.positif + rating.negatif) - 1.96 * Math.Sqrt((rating.positif * rating.negatif) / (rating.positif + rating.negatif) + 0.9604) / (rating.positif + rating.negatif)) / (1 + 3.8416 / (rating.positif + rating.negatif));
Can someone help me if this coding is correct conversion and if the sql version is actually correct.
You can store the lower-bound value as a double, there is no need to round it. The C# function can be turned into something like this:
private static double WilsonAlgorithm(double positive, double negative)
{
return ((positive + 1.9208) / (positive + negative) -
1.96 * Math.Sqrt((positive * negative) / (positive + negative) + 0.9604) /
(positive + negative)) / (1 + 3.8416 / (positive + negative));
}
(credits to: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html, most of it came from the sql code on his page)
To convert from stars --> positive/negative, you can calculate them without over-using the ternary operator for readability.
// constants
const int maxRating = 5;
const int minRating = 1;
const double shareRating = 0.25;
// conversions
var stars = 5;
var positive = (stars - minRating) * shareRating;
var negative = (maxRating - stars) * shareRating;
// ..
// usage?
var lowerBoundRating = WilsonAlgorithm(totalPositive, totalNegative);

Categories