I have a list of strings in C#, and want to create a list of unique characters that are in the strings in the list, using LINQ.
I have so far worked out how to turn the List into a List, but I can't work out how to get the LINQ to go further than that.
What I have so far is as follows:
List<string> dictionary = new List<string>(someArray);
List<string[]> uniqueCharacters = dictionary.ConvertAll(s => s.Split());
I believe I need to something along the lines of
List<char> uniqueCharacters =
dictionary.ConvertAll(s => s.Split()).SelectAll(t, i=>t[i][0]);
Compared to SQL, LINQ is simpler, tidier, and higher-level. It's rather like comparing C# to C++. Sure, there are times when it's still best to use C++ (as is the case with SQL), but in most situations, working in a modern tidy language and not having to worry about lower-level details is a big win.
No it is not.
LINQ is the basic C#. It is utilized to recover information from various kinds of sources, for example, XML, docs, collections, ADO.Net DataSet, Web Service, MS SQL Server, and different database servers.
LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL like query syntaxes. LINQ provides functions to query cached data from all kinds of data sources. The data source could be a collection of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable<T> interface.
You use the same basic query expression patterns to query and transform data in SQL databases, ADO.NET Datasets, XML documents and streams, and .NET collections. You can write LINQ queries in C# for SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable<T> interface.
Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.
The LINQ to XML functionality can be done by using the System.Xml.Linq namespace. In this article, we'll work with the three techniques LINQ to Objects, LINQ to SQL, and LINQ to DataSets. The term LINQ to Objects refers to the use of LINQ queries to access in-memory data structures. You can query any type that supports IEnumerable<T>.
You can use LINQ's SelectMany method, e.g.:
var list = new List<string> { "Foo", "Bar" };
var chars = list.SelectMany(s => s.ToCharArray());
var distinct = chars.Distinct();
Get your LinQ result and put it in loop, compare every char with in list of char.
foreach (string character in dictionary)
{
if (!(uniqueCharacters).Contains(character))
{
uniqueCharacters.Add(character);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With