Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using LINQ in C# to convert a List<string> to a List<char>

Tags:

c#

list

linq

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]);
like image 974
simonalexander2005 Avatar asked Jul 02 '10 07:07

simonalexander2005


People also ask

Why we use LINQ instead of SQL?

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.

Is LINQ obsolete?

No it is not.

What is LINQ explain with example?

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.

What is LINQ in C++?

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.

Can I use LINQ in SQL Server?

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.

What is LINQ in IntelliSense?

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.

How do I use LINQ to XML?

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>.


2 Answers

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();
like image 97
Matthew Abbott Avatar answered Oct 11 '22 12:10

Matthew Abbott


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);
            }
        }
like image 26
Serkan Hekimoglu Avatar answered Oct 11 '22 14:10

Serkan Hekimoglu