深入理解 LINQ to Objects:从基础到高级应用
1. 不使用 LINQ 的数据查询
LINQ 并非强制使用的技术。若不使用它,也能借助编程基础元素(如 if 语句和 for 循环)来获取相同的结果集。以下代码以更冗长的方式实现了与 QueryOverStrings() 方法相同的功能:
static void QueryOverStringsLongHand()
{
// 假设我们有一个字符串数组
string[] currentVideoGames = { "Morrowind", "Uncharted 2",
"Fallout 3", "Daxter", "System Shock 2" };
string[] gamesWithSpaces = new string[5];
for (int i = 0; i < currentVideoGames.Length; i++)
{
if (currentVideoGames[i].Contains(" "))
gamesWithSpaces[i] = currentVideoGames[i];
}
// 对结果进行排序
Array.Sort(gamesWithSpaces);
// 输出结果
foreach (string s in gamesWithSpaces)
{
if (s != null)
超级会员免费看
订阅专栏 解锁全文
15

被折叠的 条评论
为什么被折叠?



