C#, Convert/Join List of arrays into single array [duplicate] - c#

This question already has answers here:
Best way to combine two or more byte arrays in C#
(13 answers)
Closed 1 year ago.
I have the following list of arrays
var list = new List<string[]>();
how to use LINQ to convert/Join them into a single array
string[] singleArray;

It can be done by SelectMany operator.
var singleArray = list.SelectMany(x => x).ToArray()

Related

How can I convert a string to an int array in C#? [duplicate]

This question already has answers here:
Convert string numbers to Array C#
(3 answers)
Closed 2 years ago.
for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?
Without any error checking you can do:
var value = "231143";
var array = value.Select(c => c - '0').ToArray();
This makes use of a trick whereby you can subtract '0' from the value of a single character holding a number to get its integer value.

Need some help sorting an array of structures in c# [duplicate]

This question already has answers here:
How to use Array.sort to sort an array of structs, by a specific element
(3 answers)
How would I sort through an array of structs?
(3 answers)
Closed 6 years ago.
Hello I would Like to receive some help. I have this structure:
struct data
{
public String names;
public int number;
}
I've been asked to show this structure in the console sorted alphabetically (by evaluating the names) I don't really know how to do this, I know how to sort arrays but i don't know how to sort a structure like this.
I am a beginner, any help is received thanks.
this might do the trick for you
data[] datas = new[] {
new data() { names = "Mohit", number = 3 },
//More data like that
}
and then
Array.Sort<data>(datas, (x,y) => x.names.CompareTo(y.names));
//or
Array.Sort(datas, (x,y) => string.Compare(x.names, y.names));
Or by using System.Linq
datas.OrderBy(x=>x.names);

Determine List.IndexOf ignoring case [duplicate]

This question already has answers here:
Case-Insensitive List Search
(8 answers)
Closed 6 years ago.
Is there a way to get the index of a item within a List with case insensitive search?
List<string> sl = new List<string>() { "a","b","c"};
int result = sl.IndexOf("B"); // should be 1 instead of -1
Try this : So there is no direct way to use IndexOf with String Comparison option for LIST, to achieve desire result you need to use Lambda expression.
int result = sl.FindIndex(x => x.Equals("B",StringComparison.OrdinalIgnoreCase));
The IndexOf method for Strings in C# has a ComparisonType argument, which should work something like this:
sl.IndexOf("yourValue", StringComparison.CurrentCultureIgnoreCase)
or
sl.IndexOf("yourValue", StringComparison.OrdinalIgnoreCase)
Documentation for this can be found here and here

change string type list to type integer [duplicate]

This question already has answers here:
Converting a List<String> to List<int>
(2 answers)
Closed 9 years ago.
The list is below: I simply want to convert it to an integer list
List<string> list2 = new List<string>();
How do I convert all of it's contents to int?
var intList = list2.Select(x => int.Parse(x)).ToList();
You can use List.ConvertAll<int>:
List<int> ints = list2.ConvertAll<int>(int.Parse);
If you don't have a list you could use a Select with int.Parse:
List<int> ints = strings.Select(s=> int.Parse(s)).ToList();

Compare Values of 2 Lists C# [duplicate]

This question already has answers here:
Compare two List<T> objects for equality, ignoring order [duplicate]
(9 answers)
Closed 9 years ago.
I want to compare the values of two lists for a program I'm making. I want it to compare the 1st value of List 1 to the first value of List 2, and then the second value of List 1 to the second value of List 2, and so on.
How would I go about doing this in C#?
There is a special method for this, called SequenceEqual:
IList<int> myList1 = new List<int>(...);
IList<int> myList2 = new List<int>(...);
if (myList1.SequenceEqual(list2)) {
...
}
You can do custom comparison of sequences using the Zip method. For example, to see if any pair is not within the difference of three, you can do this:
IList<int> myList1 = new List<int>(...);
IList<int> myList2 = new List<int>(...);
if (myList1.Zip(list2, (a, b) => Math.Abs(a - b)).Any(diff => diff > 3)) {
...
}

Categories