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 });
}
}
Related
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}";
}
}
}
Having a List<double[]> with data
List<double[]> sqlResult = new List<double[]>();
sqlResult.Add(new double[] { 11, 21, 31 });
sqlResult.Add(new double[] { 12, 22, 32 });
sqlResult.Add(new double[] { 13, 23, 33 });
sqlResult.Add(new double[] { 14, 24, 34 });
How can I multiply the third element of each row by -1 so I would get
{ 11, 21, -31 }
{ 12, 22, -32 }
{ 13, 23, -33 }
{ 14, 24, -34 }
Do not know how to do it with LINQ
sqlResult= sqlResult.Select(item => -item[2]).ToList();
If you avoid access by index, you won't get a exception if there are less than 3 elements within your List.
sqlResult = sqlResult.Select(x => x.Select((y, i) => i == 2 ? -y : y).ToArray()).ToList();
var result = sqlResult.Select(x => new double[] { x[0], x[1], x[2] * -1}).ToList();
Try this
sqlResult.ForEach(y => y[2]= y[2] * -1);
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; }
}
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);
}
}
I would like to know how to take one element from each item in a list and put it into an array for graphing purposes. I was wondering if, since I want to graph each element in each row of the list, could I do it all at once or would I have to separately pull each element out into its own array?
A few lines of a code example is my preferred learning method and would be much appreciated.
Thanks!
You can conceptually put the logic to do this in one place.
class DataIterator<T> : IEnumerable<T[]> {
private readonly IList<IList<T>> _lists;
public DataIterator(IList<IList<T>> lists) {
Contract.Assert(lists.All(l => l.Count == lists[0].Count));
_lists = lists;
}
public IEnumerator<T[]> GetEnumerator() {
var value = new List<T>(_lists.Count);
for (var i = 0; i < _lists[0].Count; i++) {
value.AddRange(_lists.Select(t => t[i]));
yield return value.ToArray();
value = new List<T>(_lists.Count);
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
This implements IEnumerable<T> so you can use it which foreach loops.
An example of use:
var lists = new List<IList<int>> {
new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
new List<int> { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 },
new List<int> { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }
};
var iter = new DataIterator<int>(lists);
foreach (var items in iter) {
Array.ForEach(items, i => {
Console.Write("{0:D2} ", i);
});
Console.WriteLine();
}
outputs:
01 11 21
02 12 12
...