LINQ and Strings

LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the Split or Split method to create an array of strings that you can then query or modify by using LINQ. You can use the IsMatch method in the where clause of a LINQ query. And you can use LINQ to query or modify the MatchCollection results returned by a regular expression.

You can also use the techniques described in this section to transform semi-structured text data to XML. For more information, see How to: Generate XML from CSV Files.

The examples in this section fall into two categories:

Querying a Block of Text

You can query, analyze, and modify text blocks by splitting them into a queryable array of smaller strings by using the Split method or the Split method. You can split the source text into words, sentences, paragraphs, pages, or any other criteria, and then perform additional splits if they are required in your query.

Querying Semi-Structured Data in Text Format

Many different types of text files consist of a series of lines, often with similar formatting, such as tab- or comma-delimited files or fixed-length lines. After you read such a text file into memory, you can use LINQ to query and/or modify the lines. LINQ queries also simplify the task of combining data from multiple sources.

See Also


How to: Generate XML from CSV Files

Other Resources

LINQ (Language-Integrated Query)