Sau đây là các chuỗi của chúng tôi trong danh sách -
List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" }; Để sử dụng 3 chữ cái đầu tiên, hãy sử dụng phương thức chuỗi con và sử dụng nó theo phương pháp Linq Select.
IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str => str.Substring(0, 3));
Ví dụ
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" };
// getting first 3 letters from every string
IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str => str.Substring(0,3));
foreach (string str in res) {
Console.WriteLine(str);
}
}
} Đầu ra
key mou joy mon