Como classificar ou filtrar dados de texto por qualquer palavra ou campo (LINQ) (C#)How to sort or filter text data by any word or field (LINQ) (C#)
O exemplo a seguir mostra como classificar linhas de texto estruturado, como valores separados por vírgulas, por qualquer campo na linha.The following example shows how to sort lines of structured text, such as comma-separated values, by any field in the line. O campo pode ser especificado dinamicamente em runtime.The field may be dynamically specified at runtime. Suponha que os campos em scores.csv representam o número de ID do aluno, seguido por uma série de quatro resultados de teste.Assume that the fields in scores.csv represent a student's ID number, followed by a series of four test scores.
Para criar um arquivo que contém dadosTo create a file that contains data
- Copie os dados de scores.csv do tópico como unir conteúdo de arquivos diferentes (LINQ) (C#) e salve-o em sua pasta de solução.Copy the scores.csv data from the topic How to join content from dissimilar files (LINQ) (C#) and save it to your solution folder.
ExemploExample
public class SortLines
{
static void Main()
{
// Create an IEnumerable data source
string[] scores = System.IO.File.ReadAllLines(@"../../../scores.csv");
// Change this to any value from 0 to 4.
int sortField = 1;
Console.WriteLine("Sorted highest to lowest by field [{0}]:", sortField);
// Demonstrates how to return query from a method.
// The query is executed here.
foreach (string str in RunQuery(scores, sortField))
{
Console.WriteLine(str);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
// Returns the query variable, not query results!
static IEnumerable<string> RunQuery(IEnumerable<string> source, int num)
{
// Split the string and sort on field[num]
var scoreQuery = from line in source
let fields = line.Split(',')
orderby fields[num] descending
select line;
return scoreQuery;
}
}
/* Output (if sortField == 1):
Sorted highest to lowest by field [1]:
116, 99, 86, 90, 94
120, 99, 82, 81, 79
111, 97, 92, 81, 60
114, 97, 89, 85, 82
121, 96, 85, 91, 60
122, 94, 92, 91, 91
117, 93, 92, 80, 87
118, 92, 90, 83, 78
113, 88, 94, 65, 91
112, 75, 84, 91, 39
119, 68, 79, 88, 92
115, 35, 72, 91, 70
*/
Este exemplo também demonstra como retornar uma variável de consulta de um método.This example also demonstrates how to return a query variable from a method.
Compilando o códigoCompiling the Code
Criar um projeto de aplicativo de console em C# com diretivas using
para os namespaces System.Linq e System.IO.Create a C# console application project, with using
directives for the System.Linq and System.IO namespaces.