查询包含一组指定词语的句子
1 class FindSentences
2 {
3 static void Main()
4 {
5 string text = @"Historically, the world of data and the world of objects " +
6 @"have not been well integrated. Programmers work in C# or Visual Basic " +
7 @"and also in SQL or XQuery. On the one side are concepts such as classes, " +
8 @"objects, fields, inheritance, and .NET Framework APIs. On the other side " +
9 @"are tables, columns, rows, nodes, and separate languages for dealing with " +
10 @"them. Data types often require translation between the two worlds; there are " +
11 @"different standard functions. Because the object world has no notion of query, a " +
12 @"query can only be represented as a string without compile-time type checking or " +
13 @"IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " +
14 @"objects in memory is often tedious and error-prone.";
15
16 // Split the text block into an array of sentences.
17 string[] sentences = text.Split(new char[] { '.', '?', '!' });
18
19 // Define the search terms. This list could also be dynamically populated at runtime.
20 string[] wordsToMatch = { "Historically", "data", "integrated" };
21
22 // Find sentences that contain all the terms in the wordsToMatch array.
23 // Note that the number of terms to match is not specified at compile time.
24 var sentenceQuery = from sentence in sentences
25 let w = sentence.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
26 StringSplitOptions.RemoveEmptyEntries)
27 where w.Distinct().Intersect(wordsToMatch).Count() == wordsToMatch.Count()
28 select sentence;
29
30 // Execute the query. Note that you can explicitly type
31 // the iteration variable here even though sentenceQuery
32 // was implicitly typed.
33 foreach (string str in sentenceQuery)
34 {
35 Console.WriteLine(str);
36 }
37
38 // Keep the console window open in debug mode.
39 Console.WriteLine("Press any key to exit");
40 Console.ReadKey();
41 }
42 }
43 /* Output:
44 Historically, the world of data and the world of objects have not been well integrated
45 */
2 {
3 static void Main()
4 {
5 string text = @"Historically, the world of data and the world of objects " +
6 @"have not been well integrated. Programmers work in C# or Visual Basic " +
7 @"and also in SQL or XQuery. On the one side are concepts such as classes, " +
8 @"objects, fields, inheritance, and .NET Framework APIs. On the other side " +
9 @"are tables, columns, rows, nodes, and separate languages for dealing with " +
10 @"them. Data types often require translation between the two worlds; there are " +
11 @"different standard functions. Because the object world has no notion of query, a " +
12 @"query can only be represented as a string without compile-time type checking or " +
13 @"IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " +
14 @"objects in memory is often tedious and error-prone.";
15
16 // Split the text block into an array of sentences.
17 string[] sentences = text.Split(new char[] { '.', '?', '!' });
18
19 // Define the search terms. This list could also be dynamically populated at runtime.
20 string[] wordsToMatch = { "Historically", "data", "integrated" };
21
22 // Find sentences that contain all the terms in the wordsToMatch array.
23 // Note that the number of terms to match is not specified at compile time.
24 var sentenceQuery = from sentence in sentences
25 let w = sentence.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
26 StringSplitOptions.RemoveEmptyEntries)
27 where w.Distinct().Intersect(wordsToMatch).Count() == wordsToMatch.Count()
28 select sentence;
29
30 // Execute the query. Note that you can explicitly type
31 // the iteration variable here even though sentenceQuery
32 // was implicitly typed.
33 foreach (string str in sentenceQuery)
34 {
35 Console.WriteLine(str);
36 }
37
38 // Keep the console window open in debug mode.
39 Console.WriteLine("Press any key to exit");
40 Console.ReadKey();
41 }
42 }
43 /* Output:
44 Historically, the world of data and the world of objects have not been well integrated
45 */