c# - 遍历 string[] List<string> 按字母顺序获取结果

标签 c# linq list arrays

我将首先在这里声明主要问题,因此您可以根据需要跳过其余部分。

如何使结果列表按字母顺序排序?

public static List<string> GenerateMonthNames(string prefixText)
{
    List<string> items = new List<string>();
    items.Add("a");
    items.Add("appl");
    items.Add("ap");
    items.Add("apple");
    items.Add("change");
    items.Add("world");
    items.Add("engaging");

    string[] strArray = items.ToArray();

    string tempFundstr = string.Empty;
    List<string> returnedList= new List<string>();

    int strNumber;
    int strIndex = 0;
    for (strNumber = 0; strNumber < strArray.Length; strNumber++)
    {
        strIndex = strArray[strNumber].IndexOf(prefixText);
        if (strIndex >= 0)
        {
            tempFoundStr = strArray[strNumber];
            retLst.Add(retstr);
        }    
    }

    items.Clear();
    return returnedList;
}

在 Ajax autoComplete 上进行绝望的尝试只是......让它工作,而不使用数据库作为自动完成数据的来源

尽管我已经解决了主要问题,因为它根本不起作用(;。现在在“修复”问题之后,autoComplete 确实起作用了。但是

这是一个困惑的解决方案,我设法通过列表或数组进行迭代,这里的问题是我无法理解如何通过使用 Linq、枚举或任何实现,所以我将能够返回结果列表按字母顺序排序

在您的帮助下,我希望(按照书本)弄清楚代码。

最佳答案

这应该可行

items.Where(item => item.IndexOf(prefixText) >= 0).OrderBy(item => item)

关于c# - 遍历 string[] List<string> 按字母顺序获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490337/

相关文章:

c# - SQL "IN"CLAUSE 性能问题

c# - 列表中的SelectMany:“无法根据使用情况推断类型自变量”

java - 从 Java 中的列表打印模式

Python:将一个属性修改为列表中的每个对象

c# - DynamicMock 和预期#1,实际#0

c# - 从 C# 应用程序调用 ASP.net Web 服务

c# - 使用 List<Person> Distinct() 返回 2 个值

c - 访问 *head 并从文件中读取

C#/.NET XML 序列化程序 - 使用属性作为元素名称

c# - 如何在不迭代的情况下在包装时移动对象数组?