TypeScript array equivalent in C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this array in TypeScript:
public lineChartData:Array<any> = [
{data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A'},
{data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'},
{data: [18, 48, 77, 9, 100, 27, 40], label: 'Series C'}
];
How would I go about creating the equivalent in C#? I'm new to TypeScript and have never come across the 'any' type.

With anonymous types you could have
var lineChartData = new [] {
new { data = new [] { 65, 59, 80, 81, 56, 55, 40 }, label = "Series A" },
new { data = new [] { 65, 59, 80, 81, 56, 55, 40 }, label = "Series B" }
};
That looks quite close to the Typescript version presented in the question.

It'll be something like this in C#:
var lineChartData = new List<DataLine>
{
new DataLine {Data = new[] {65, 59, 80, 81, 56, 55, 40}, Label = "Series A"},
new DataLine {Data = new[] {28, 48, 40, 19, 86, 27, 90}, Label = "Series B"},
new DataLine {Data = new[] {18, 48, 77, 9, 100, 27, 40}, Label = "Series C"}
};
And every line of your LineChartData will be something like this:
class DataLine
{
public int[] Data { get; set; }
public string Label { get; set; }
}

Related

Group by descending average test score?

I need help in LINQ, I need to group a list of students based on the calculated average in a rage
There is a student class :
public class Student
{
public string Name { get; set; } // student name
public int[] Scores { get; set; } // test scores
}
and I have a list of students with the data
List<Student> students = new List<Student>
{
new Student { Name = "Michael", Scores = new int[] { 94, 92, 91, 91 } },
new Student { Name = "Isabelle", Scores = new int[] { 66, 87, 65, 93, 86} },
new Student { Name = "Chastity", Scores = new int[] { 76, 61, 73, 66, 54} },
new Student { Name = "Chaim", Scores = new int[] { 94, 55, 82, 62, 52} },
new Student { Name = "Patience", Scores = new int[] { 91, 79, 58, 63, 55} },
new Student { Name = "Echo", Scores = new int[] { 74, 85, 73, 75, 86} },
new Student { Name = "Pamela", Scores = new int[] { 73, 64, 53, 72, 68} },
new Student { Name = "Anne", Scores = new int[] { 78, 96, 52, 79, 60} },
new Student { Name = "Fuller", Scores = new int[] { 59, 68, 88, 85, 76} },
new Student { Name = "Cameron", Scores = new int[] { 70, 73, 75, 51, 98} },
new Student { Name = "Aurora", Scores = new int[] { 65, 70, 53, 80, 52} },
new Student { Name = "Anthony", Scores = new int[] { 68, 69, 94, 88, 98} },
}
the bracket I need to range is in Test score brackets are in multiples of 10
• 50, 60, 70, 80, 90, 100
The output should look like:
Something like that I suppose ?
Result:
--------------------------
90
Name: Michael
--------------------------
--------------------------
80
Name: Isabelle
Name: Echo
Name: Fuller
Name: Anthony
--------------------------
--------------------------
70
Name: Chastity
Name: Chaim
Name: Patience
Name: Pamela
Name: Anne
Name: Cameron
--------------------------
--------------------------
60
Name: Aurora
--------------------------
Code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
internal static class Program
{
private static void Main()
{
var students = new List<Student>
{
new() {Name = "Michael", Scores = new[] {94, 92, 91, 91}},
new() {Name = "Isabelle", Scores = new[] {66, 87, 65, 93, 86}},
new() {Name = "Chastity", Scores = new[] {76, 61, 73, 66, 54}},
new() {Name = "Chaim", Scores = new[] {94, 55, 82, 62, 52}},
new() {Name = "Patience", Scores = new[] {91, 79, 58, 63, 55}},
new() {Name = "Echo", Scores = new[] {74, 85, 73, 75, 86}},
new() {Name = "Pamela", Scores = new[] {73, 64, 53, 72, 68}},
new() {Name = "Anne", Scores = new[] {78, 96, 52, 79, 60}},
new() {Name = "Fuller", Scores = new[] {59, 68, 88, 85, 76}},
new() {Name = "Cameron", Scores = new[] {70, 73, 75, 51, 98}},
new() {Name = "Aurora", Scores = new[] {65, 70, 53, 80, 52}},
new() {Name = "Anthony", Scores = new[] {68, 69, 94, 88, 98}}
};
var groups = students.GroupBy(s => Math.Round(s.Scores.Average() / 10) * 10).OrderByDescending(s => s.Key);
foreach (var grouping in groups)
{
Console.WriteLine("--------------------------");
Console.WriteLine(grouping.Key);
foreach (var student in grouping)
{
Console.WriteLine(student);
}
Console.WriteLine("--------------------------");
Console.WriteLine();
}
}
}
public class Student
{
public string Name { get; set; }
public int[] Scores { get; set; }
public override string ToString()
{
return $"{nameof(Name)}: {Name}";
}
}
}

How to decode a string encoded by C# Convert.ToBase64String in python 3

The c# server side:
[HttpGet("{id}")]
public ActionResult Get(int id)
{
var user = new User
{
Id = id,
Name = $"User{id}"
};
using(var ms = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms, user);
var bytes = ms.ToArray();
var str = Convert.ToBase64String(bytes);
return Json(str);
}
}
the python client side:
async def foo():
async with ClientSession() as session:
async with session.get("http://localhost:57968/api/values/5") as response:
json = await response.json()
# how to get the bytes created by the server
person = Person.create_from_bytes(bs)
How to get the raw bytes array created by the server using ProtoBuf.Serializer.Serialize(ms, user); in the python client.
If i do not wrap the raw byte array with base64:
Update: I worked it out in the python client side like this:
json = await response.json()
bs = json.encode("ascii")
b64 = base64.b64decode(bs)
person = Person.create_from_bytes(b64)
print(f"{person.id} {person.name}")
Finally, i worked it out in the client side by using:
json = await response.json()
bs = json.encode("ascii")
b64 = base64.b64decode(bs)
person = Person.create_from_bytes(b64)
print(f"{person.id} {person.name}")
I had some time to develop this Solution for Python3.7, considering as example:
''' Argument = [110, 13, 46, 136, 95, 66, 92, 132, 109, 217, 58, 112, 43, 8, 145,
42, 233, 98, 40, 139, 165, 228, 52, 9, 89, 175, 146, 103, 227, 238, 233, 190,
78, 175, 242, 224, 202, 138, 248, 103, 114, 98, 199, 252, 80, 86, 61, 174]
return = 'bg0uiF9CXIRt2TpwKwiRKuliKIul5DQJWa+SZ+Pu6b5Or/Lgyor4Z3Jix/xQVj2u'
'''
Like this:
import base64
import numpy as np
# Do the same as Convert.ToBase64String() from C#
def ConvertToBase64String(encrypted):
return (base64.b64encode(textoUnicodeToUtf8Literal(("".join([chr(item) for item in np.array(encrypted, dtype=np.uint8)]))).encode('ISO-8859-1'))).decode()
# Do the oposite of Convert.ToBase64String() from C#
def ConvertToStringBase64(encrypted):
return np.frombuffer(base64.b64decode(encrypted.encode()), np.uint8)
def textoUnicodeToUtf8Literal(encodando):
return encodando.replace("\xc2\x80", r'\x80').replace("\xc2\x81", r'\x81').replace("\xc2\x82", r'\x82')\
.replace("\xc2\x83", r'\x83').replace("\xc2\x84", r'\x84').replace("\xc2\x85", r'\x85')\
.replace("\xc2\x86", r'\x86').replace("\xc2\x87", r'\x87').replace("\xc2\x88", r'\x88')\
.replace("\xc2\x89", r'\x89').replace("\xc2\x8a", r'\x8A').replace("\xc2\x8b", r'\x8B')\
.replace("\xc2\x8c", r'\x8C').replace("\xc2\x8d", r'\x8D').replace("\xc2\x8e", r'\x8E')\
.replace("\xc2\x8f", r'\x8F').replace("\xc2\x90", r'\x90').replace("\xc2\x91", r'\x91')\
.replace("\xc2\x92", r'\x92').replace("\xc2\x93", r'\x93').replace("\xc2\x94", r'\x94')\
.replace("\xc2\x95", r'\x95').replace("\xc2\x96", r'\x96').replace("\xc2\x97", r'\x97')\
.replace("\xc2\x98", r'\x98').replace("\xc2\x99", r'\x99').replace("\xc2\x9a", r'\x9A')\
.replace("\xc2\x9b", r'\x9B').replace("\xc2\x9c", r'\x9C').replace("\xc2\x9d", r'\x9D')\
.replace("\xc2\x9e", r'\x9E').replace("\xc2\x9f", r'\x9F').replace("\xc2\xa0", r'\xA0')\
.replace("\xc2\xa1", r'\xA1').replace("\xc2\xa2", r'\xA2').replace("\xc2\xa3", r'\xA3')\
.replace("\xc2\xa4", r'\xA4').replace("\xc2\xa5", r'\xA5').replace("\xc2\xa6", r'\xA6')\
.replace("\xc2\xa7", r'\xA7').replace("\xc2\xa8", r'\xA8').replace("\xc2\xa9", r'\xA9')\
.replace("\xc2\xaa", r'\xAA').replace("\xc2\xab", r'\xAB').replace("\xc2\xac", r'\xAC')\
.replace("\xc2\xad", r'\xAD').replace("\xc2\xae", r'\xAE').replace("\xc2\xaf", r'\xAF')\
.replace("\xc2\xb0", r'\xB0').replace("\xc2\xb1", r'\xB1').replace("\xc2\xb2", r'\xB2')\
.replace("\xc2\xb3", r'\xB3').replace("\xc2\xb4", r'\xB4').replace("\xc2\xb5", r'\xB5')\
.replace("\xc2\xb6", r'\xB6').replace("\xc2\xb7", r'\xB7').replace("\xc2\xb8", r'\xB8')\
.replace("\xc2\xb9", r'\xB9').replace("\xc2\xba", r'\xBA').replace("\xc2\xbb", r'\xBB')\
.replace("\xc2\xbc", r'\xBC').replace("\xc2\xbd", r'\xBD').replace("\xc2\xbe", r'\xBE')\
.replace("\xc2\xbf", r'\xBF').replace("\xc3\x80", r'\xC0').replace("\xc3\x81", r'\xC1')\
.replace("\xc3\x82", r'\xC2').replace("\xc3\x83", r'\xC3').replace("\xc3\x84", r'\xC4')\
.replace("\xc3\x85", r'\xC5').replace("\xc3\x86", r'\xC6').replace("\xc3\x87", r'\xC7')\
.replace("\xc3\x88", r'\xC8').replace("\xc3\x89", r'\xC9').replace("\xc3\x8a", r'\xCA')\
.replace("\xc3\x8b", r'\xCB').replace("\xc3\x8c", r'\xCC').replace("\xc3\x8d", r'\xCD')\
.replace("\xc3\x8e", r'\xCE').replace("\xc3\x8f", r'\xCF').replace("\xc3\x90", r'\xD0')\
.replace("\xc3\x91", r'\xD1').replace("\xc3\x92", r'\xD2').replace("\xc3\x93", r'\xD3')\
.replace("\xc3\x94", r'\xD4').replace("\xc3\x95", r'\xD5').replace("\xc3\x96", r'\xD6')\
.replace("\xc3\x97", r'\xD7').replace("\xc3\x98", r'\xD8').replace("\xc3\x99", r'\xD9')\
.replace("\xc3\x9a", r'\xDA').replace("\xc3\x9b", r'\xDB').replace("\xc3\x9c", r'\xDC')\
.replace("\xc3\x9d", r'\xDD').replace("\xc3\x9e", r'\xDE').replace("\xc3\x9f", r'\xDF')\
.replace("\xc3\xa0", r'\xE0').replace("\xc3\xa1", r'\xE1').replace("\xc3\xa2", r'\xE2')\
.replace("\xc3\xa3", r'\xE3').replace("\xc3\xa4", r'\xE4').replace("\xc3\xa5", r'\xE5')\
.replace("\xc3\xa6", r'\xE6').replace("\xc3\xa7", r'\xE7').replace("\xc3\xa8", r'\xE8')\
.replace("\xc3\xa9", r'\xE9').replace("\xc3\xaa", r'\xEA').replace("\xc3\xab", r'\xEB')\
.replace("\xc3\xac", r'\xEC').replace("\xc3\xad", r'\xED').replace("\xc3\xae", r'\xEE')\
.replace("\xc3\xaf", r'\xEF').replace("\xc3\xb0", r'\xF0').replace("\xc3\xb1", r'\xF1')\
.replace("\xc3\xb2", r'\xF2').replace("\xc3\xb3", r'\xF3').replace("\xc3\xb4", r'\xF4')\
.replace("\xc3\xb5", r'\xF5').replace("\xc3\xb6", r'\xF6').replace("\xc3\xb7", r'\xF7')\
.replace("\xc3\xb8", r'\xF8').replace("\xc3\xb9", r'\xF9').replace("\xc3\xba", r'\xFA')\
.replace("\xc3\xbb", r'\xFB').replace("\xc3\xbc", r'\xFC').replace("\xc3\xbd", r'\xFD')\
.replace("\xc3\xbe", r'\xFE').replace("\xc3\xbf", r'\xFF')

List method to get difference of another list with duplicates (listobject.Expect method does not work)

There are two Lists. I need the difference
List<int> list1 = new List<int>() {18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() {18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
var listDif = list2.Except(list1);
foreach (var s in listDif)
Console.WriteLine(s);
Console.Read();
the answer should be 20, 86,78, 25
but it only outputs 86,78
If you want exactly that kind of behaviour you should try this:
List<int> list1 = new List<int>() { 18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() { 18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
// Remove elements of first list from second list
list1.ForEach(l => list2.Remove(l));
list2 = list2.Distinct().ToList();
list2.ForEach(d => Console.WriteLine(d));
Console.Read();
This works fine:
Make a clone of list2
Remove list1 items from list2
Code Sample:
List<int> list1 = new List<int>() { 18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() { 18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
var diff = list2;
list1.All(x => diff.Remove(x));
You can also perform Remove on list2, however, that will modify list2.
Because you only check which numbers in list1 are missing in list2
But you need to check which numbers in list2 doesn't exist in list1 and in
listDif.
You can do
List<int> diff = new List<int>();
foreach (int num in list1)
{
if (!list2.Contains(num))
{
diff.Add(num);
}
}
foreach (int num in list2)
{
if (!list1.Contains(num) && !diff.contains(num))
{
diff.Add(num);
}
}

Adding many lists

If I have 100 lists (eg x1 to x100), is there a better way of expressing the final line of code?
var x1 = new List<int>() { 75 };
var x2 = new List<int>() { 95, 64 };
var x3 = new List<int>() { 17, 47, 82 };
var x4 = new List<int>() { 18, 35, 87, 10 };
var x5 = new List<int>() { 20, 04, 82, 47, 65 };
var x6 = new List<int>() { 19, 01, 23, 75, 03, 34 };
var x7 = new List<int>() { 88, 02, 77, 73, 07, 63, 67 };
//etc..
var listOfListOfInts = new List<List<int>>() { x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 };
possibly a Dictionary and a for loop to reference all the x1..100.
As long as x1 etc. aren't referenced elsewhere, then write:
var listOfListOfInts = new List<List<int>>()
{
new List<int>() { 75 },
new List<int>() { 95, 64 },
//etc.
};
In fact you shouldn't need to reference the individual variables elsewhere since listOfListOfInts[0] is just as good as x1 for example.
Do you really need these to be of type List<T>? It looks like you're setting up preinitialized data. If you're never going to change the length of any of these "lists", you could use arrays instead; the syntax is more compact:
var listOfListOfInts = new[] {
new[] { 75 },
new[] { 95, 64 },
new[] { 17, 47, 82 },
new[] { 18, 35, 87, 10 },
new[] { 20, 04, 82, 47, 65 },
new[] { 19, 01, 23, 75, 03, 34 },
new[] { 88, 02, 77, 73, 07, 63, 67 },
// ...
};
Perhaps i'm over complicating things but you could do something like
public interface IClass1
{
IList<IList<int>> ListList { get; set; }
void AddList(List<int> nList);
}
public class Class1 : IClass1
{
public IList<IList<int>> ListList { get; set; }
public void AddList(List<int> nList)
{
ListList.Add(nList);
}
}
and then use it like:
public class Create1
{
public Create1()
{
IClass1 iClass1 = new Class1();
iClass1.AddList(new List<int>() { 75 });
}
}

How to convert ASCII data to EBCDIC in C#?

I read about conversion of ASCII to EBCDIC using this link;
Convert String from ASCII to EBCDIC in Java?
But this is in java. My requirement is in C#.Net.
So can you please help me with this?
Thanks & Regards,
Krishna Kumar
Here's an implementation (by #Jon Skeet) you might find useful.
I tried the above code, and found it did not work for me.
For example, '04' (0x3034) ascii translated to 0x00f000f4 ebcdic. Problem seemed to be the encoding.
Played with several approches to change this, but finally found the best solution was the most basic. Instead of using Convert.ToChar, I plugged the hex value into the array.
Please see following:
static byte[] ASCIItoEBCDIC(string asciiString)
{
byte[] asciiToEbcdicTable = new byte[256] {
0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x25,0x0B,0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
0x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xBA,0xE0,0xBB,0xB0,0x6D,
0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
0x20,0x21,0x22,0x23,0x24,0x15,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
0x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
0x41,0xAA,0x4A,0xB1,0x9F,0xB2,0x6A,0xB5,0xBD,0xB4,0x9A,0x8A,0x5F,0xCA,0xAF,0xBC,
0x90,0x8F,0xEA,0xFA,0xBE,0xA0,0xB6,0xB3,0x9D,0xDA,0x9B,0x8B,0xB7,0xB8,0xB9,0xAB,
0x64,0x65,0x62,0x66,0x63,0x67,0x9E,0x68,0x74,0x71,0x72,0x73,0x78,0x75,0x76,0x77,
0xAC,0x69,0xED,0xEE,0xEB,0xEF,0xEC,0xBF,0x80,0xFD,0xFE,0xFB,0xFC,0xAD,0xAE,0x59,
0x44,0x45,0x42,0x46,0x43,0x47,0x9C,0x48,0x54,0x51,0x52,0x53,0x58,0x55,0x56,0x57,
0x8C,0x49,0xCD,0xCE,0xCB,0xCF,0xCC,0xE1,0x70,0xDD,0xDE,0xDB,0xDC,0x8D,0x8E,0xDF};
byte[] ebcdicBinary = new byte[asciiString.Length];
for (int pos = 0; pos < asciiString.Length; ++pos)
{
int ebcdicIndex = asciiString[pos];
ebcdicBinary[pos] = asciiToEbcdicTable[ebcdicIndex];
}
return ebcdicBinary;
}
Try like below code
public string ConvertASCIItoEBCDIC(string strASCIIString)
{
int[] a2e = new int[256]{
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};
char chrItem = Convert.ToChar("0");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strASCIIString.Length; i++)
{
try
{
chrItem = Convert.ToChar(strASCIIString.Substring(i, 1));
sb.Append(Convert.ToChar(a2e[(int)chrItem]));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return string.Empty;
}
}
string result = sb.ToString();
sb = null;
return result;
}
and check the saple code on these links
http://kseesharp.blogspot.com/2007/12/convert-ascii-to-ebcdic.html
http://forums.asp.net/t/167516.aspx
http://www.yoda.arachsys.com/csharp/ebcdic/
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c2b074fd-4293-4bf4-b7fa-1803fc625d43

Categories

Resources