42
1
using System;
2
using System.Linq;
3
using System.Collections.Generic;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
// Đoạn văn bản mẫu --- hiepsiit.com
10
// Ký hiệu @ dùng định dạng chuỗi ký tự đúng nguyên văn
11
// Ví dụ: @"C:\hiepsiit\LINQ" giống như "C:\\hiepsiit\\LINQ"
12
string text = @"LINQ extends the language by the addition of query expressions, " +
13
@" which are akin to SQL statements, and can be used to conveniently extract and " +
14
@" process data from arrays, enumerable classes, XML documents, relational databases, " +
15
@" and third-party data sources.";
16
17
// Từ khóa cần tìm kiếm
18
string searchTerm = "to";
19
20
//Chuyển đoạn văn thành các từ ngăn cách với các dấu như chấm, phẩy, ...
21
//RemoveEmptyEntries là bỏ nhiều khoảng trắng giữa các từ
22
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
23
24
// Tạo truy vấn LINQ
25
// Dùng phương thức ToLowerInvariant để so khớp hoa thường cụm từ là như nhau
26
// Ví dụ: "hiepsiit" và "hiepsiit"
27
var matchQuery = from word in source
28
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
29
select word;
30
31
// Đếm số từ khóa tìm thấy và trả về kết quả
32
int wordCount = matchQuery.Count();
33
Console.WriteLine("Tim thay tu khoa \"{0}\" xuat hien {1} trong doan van ban.", searchTerm, wordCount);
34
35
// Giữ cửa sổ trong chế độ debug
36
Console.WriteLine("Nhan bat ky phim nao de thoat chuong trinh.");
37
38
}
39
}
40
41
42
Cached Result
ingrese su nombre
>