I need to get the index of one array elements in another array using LINQ
The following are my two array:
string[] arr1 = new string[] { "Albany", "Albuquerque", "Anchorage", "Atlantic City",
"Baton Rouge", "Biloxi", "CEDAR SPRINGS", "Chicago", "Columbia", "Columbus" };
string[] arr2 = new string[] { "Albany", "Biloxi" };
Can anybody help me out on the same?
You can use Array.IndexOf:
int[] indicesOf2In1 = arr2.Select(s => Array.IndexOf(arr1, s)).ToArray(); // (0,5)
Related
Given a list<StringCollection> how do most efficiently check whether all string are contained in any of the StringCollections?
Example:
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Create and initializes a new StringCollection.
StringCollection myCol0 = new StringCollection();
StringCollection myCol1 = new StringCollection();
StringCollection myCol2 = new StringCollection();
StringCollection SearchCol = new StringCollection();
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr0 = new String[] { "RED", "car", "boat" };
myCol0.AddRange( myArr0 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr1 = new String[] { "Blue", "Goku", "Nappa" };
myCol1.AddRange( myArr1 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr2 = new String[] { "Yellow", "Winter", "Summer" };
myCol2.AddRange( myArr2 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr3 = new String[] { "Yellow", "Blue", "RED" };
SearchCol.AddRange( myArr3 );
List<StringCollection> a = new List<StringCollection>();
a.Add(myCol0);
a.Add(myCol1);
a.Add(myCol2);
}
}
In this case I want to know whether the strings in SearchCol is contained within the stringcollections stored in List<StringCollection> a
In this case I just like to know which of the searchCol strings is not included in the List<StringCollection> a
The only way I see this it possible to do so is via a double for loop?
Is there any datastructure that would be more efficient rather than an stringcollection?
Is there any datastructure that would be more efficient rather than
an stringcollection
Efficient in what way? Of course you should normally use a IEnumerable<string>(like a string[] vor List<string>) since StringCollection is not generic.
But you can also use StringCollection, you have to cast each item from object to string:
var allStrings = a.SelectMany(c => c.Cast<string>());
var searchStrings = SearchCol.Cast<string>();
bool allSearchStringsAreContained = searchStrings.All(allStrings.Contains);
as for the "how do most efficiently", this simple approach is efficient, but if you have a large list of strings that you search or huge string lists, you could use a set based approach:
HashSet<string> set = new HashSet<string>(searchStrings);
bool allSearchStringsAreContained = set.IsSubsetOf(allStrings);
Finally, if you want to ignore the case, so treat "RED" and "Red" same:
Approach 1:
bool allSearchStringsAreContained = searchStrings.All(s => allStrings.Contains(s, StringComparer.OrdinalIgnoreCase));
Approach 2:
HashSet<string> set = new HashSet<string>(searchStrings, StringComparer.OrdinalIgnoreCase);
I'd like to define a list whose element is a 3-elements array. Below codes seems ok:
List<dynamic[]> bb = null;
but when I try:
List<dynamic[3]> bb = null;
It throws error. Sure I can create a class/struct for that. But is there a way to define it directly?
Here is one way:
var list = new List<string[]>();
list.Add(new string[3] { "1", "2", "3" });
list.Add(new string[2] { "1", "2" });
Update:
#Ilya's answer shows a solution with dynamic, but I advise you against using dynamic. If you know the structure of the objects create a class or use a tuple, e.g.
var list = new List<(int id, string name, uint reputation)>();
list.Add((298540, "xiaoyafeng", 11));
list.Add((2707359, "Ilya", 3576));
list.Add((581076, "tymtam", 4421));
list.Add((3043, "Joel Coehoorn", 294378));
I'm not sure what your issue was; your title talks about a "string array list", but your posted code describes a list of arrays of dynamic.
I'm not sure I'll ever have reason to create a list of arrays of dynamic, but to show that the code that you discussed in your post can work, this works for me:
var stuff = new List<dynamic[]>
{
new dynamic[] {1, "string"},
new dynamic[] {DateTime.Now, 45.0}
};
But, as noted in other answers, dynamic is a great answer to several classes of questions. But, it's not the right answer here. The right answer to the title of your question (not the description) will use a list of string arrays (as has been pointed in the other answers:
var otherStuff = new List<string[]>
{
new string[] {"Now", "Is", "the", "time"},
new string[] {"for all", "good men, etc."}
};
It seems that you need something like this:
List<dynamic[]> bb = new List<dynamic[]>();
bb.Add(new dynamic[3]); // here you add a 3-element array
bb.Add(new dynamic[3] { "a", "b", "c" }); // here you add another one 3-element array
// List containing a string array with 3 nulls in it
var aa = new List<string[]> {new string[3]};
// List containing a string array with 3 strings in it
var bb = new List<string[]> {new[] {"a", "b", "c"}};
I've a list with some strings like this:
List<String> data = new List<String>
{
"marine",
"blue",
"SEM",
"seven",
"sensible",
"six"
};
Now I want to compare this list with a string and add the matching items to a new list:
String input = "se";
List<String> newList = new List<String>;
The matching condition is, that the first letters should be the same (case-sensitive). In this case the newList contains:
"seven" and "sensible"
How is the most performant solution?
var newList = data.Where(s => s.StartsWith(input)).ToList();
I have a jagged array of strings in C#.
How do I bind it to a DataGrid such that I can see the contents of the array?
Currently in the DataGrid, instead of the array's contents, I see a column that says "Length", "Long Length", "Rank", "SyncRoot", etc...basically, properties of the array and not the contents of the array.
My code:
string[][] jagged = new string [100][];
//...jagged array is populated...
dataGridView1.DataSource = jagged;
Here is an example that you can try following I didn't do this with String[] but you can get the Idea
//
// 1. Create two dimensional array
//
const int dim = 1000;
double[,] array = new double[dim,dim];
Random ran = new Random();
for(int r = 0; r < dim; r++)
{
for(int c = 0; c < dim; c++)
{
array[r,c] = (ran.Next(dim)); // fill it with random numbers.
}
}
// 2. Create ArrayDataView class in which
// constructor you pass the array
// and assign it to DataSource property of DataGrid.
dataGrid1.DataSource = new ArrayDataView(array);
For String[][] here is an example
string[][] arr = new string[2][];
arr[0] = new String[] {"a","b"};
arr[1] = new String[] {"c","d"};
DataGrid1.DataSource = arr[0];
DataGrid1.DataBind();//The result is: a,b in datagrid
using LinQ look at this
List<string> names = new List<string>(new string[]
{
"John",
"Frank",
"Bob"
});
var bindableNames =
from name in names
select new {Names=name};
dataGridView1.DataSource = bindableNames.ToList();
USING LINQ for Multi Denensional Array
string[][] stringRepresentation = ds.Tables[0].Rows
.OfType<DataRow>()
.Select(r => ds.Tables[0].Columns
.OfType<DataColumn>()
.Select(c => r[c.ColumnName].ToString())
.ToArray())
.ToArray();
As given by the current accepted answer and as mentioned by Michael Perrenoud in the comments, you can use Mihail Stefanov's ArrayDataView classes to achieve this binding. His original code, however, was originally conceived to work only with multidimensional arrays. I have since modified his code to work with jagged arrays as well and made it available through the Accord.NET Framework.
Now, you do not need to use the whole framework for doing that, you can simply use the updated classes available here. After incorporating those classes in your project, all you have to do is
dataGridView.DataSource = new ArrayDataView( yourArray );
I hope this clarification helps.
As I mentioned, I am the author of Accord.NET, but the original credit really goes to Stefanov.
Let's say I have following code:
List<string> numbers = new List<string> { "1", "2" };
List<string> numbers2 = new List<string> { "1", "2"};
if (numbers.Equals(numbers2))
{
}
Like you can see I have two lists with identical items. Is there a way to check if these two lists are equal by using one method?
SOLUTION:
Use SequenceEqual()
Thanks
Use Enumerable.SequenceEqual, but Sort the lists first.
// if order does not matter
bool theSame = numbers.Except(numbers2).Count() == 0;
// if order is matter
var set = new HashSet<string>(numbers);
set.SymmetricExceptWith(numbers2);
bool theSame = set.Count == 0;