c# - 使用 LINQ 在 WebService 中自动完成

标签 c# asp.net linq web-services linq-to-sql

我正在通过网络服务使用自动完成功能。在我的网络服务中,我使用的是 LDS。

问题是当我尝试这段代码时:

这是调用 Default.aspx 中的网络服务的 UI 代码:

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    <Services>
    <asp:ServiceReference Path ="WebService1.asmx" />
    </Services>
    </asp:ToolkitScriptManager> 

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
        DelimiterCharacters="" Enabled="True" ServicePath ="WebService1.asmx" ServiceMethod="ReturnEntity" EnableCaching="true"  TargetControlID="TextBox1" CompletionSetCount ="1">
    </asp:AutoCompleteExtender>

这是我的 WebService 文件 (webservice1.asmx)

 [WebMethod]
    public string[] ReturnEntity(string prefixText, int count)
    {
        List<string> responses = new List<string>();
        for (int i = 0; i < count; i++)
            responses.Add(prefixText + (char)(i + 65));

        return responses.ToArray();
    }

然后就可以了。但是,我正在处理这段代码,但它不工作......

 [WebMethod]
    public string[] ReturnEntity(string prefixText)
    {
        using (DataClasses1DataContext search = new DataClasses1DataContext())
        {
            string[] fullText = (from n in search.Entities
                                 where n.Name.StartsWith(prefixText)
                                 select n.Name).ToArray();

            return fullText;
        }

    }

当我试图在浏览器本身中运行网络服务并传递一些参数时,它会向我显示这些:

  <?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <string>Starr</string> 
  <string>Sally Jeans</string> 
  <string>Steven Kline</string> 
  <string>Steven Goldberg</string> 
  </ArrayOfString>

这不会返回任何结果。我只想显示我的表中的列表,即实体,列名称是“名称”。

无法理解:(

最佳答案

尝试使用 StartsWith并传入 StringComparison

var result = from n in search.Entities
             where n.Name.StartsWith(prefixText, 
                 StringComparison.InvariantCultureIgnoreCase)
             select n.Name;

return result.ToArray();

如果这仍在调用的上下文中,您可以使用 ToLower()

var result = from n in search.Entities
             where n.Name.ToLower().StartsWith(prefixText.ToLower())
             select n.Name;

return result.ToArray();

关于c# - 使用 LINQ 在 WebService 中自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5490744/

相关文章:

c# - LINQ for CRM 如何在 where 子句中使用 C# 列表

c# - 为什么其中一些 Monotouch.Dialog 单元格显示为空?

c# - Linq Distinct 没有带回正确的结果

c# - 自定义中间件不处理调试运行 ASP.NET CORE 的异常

c# - 使用户 session 过期(当前用户除外)

asp.net - 日历 View 中的当前月份

c# - Nancy 查看内部服务器错误

c# - 用asp传递参数:Button onClick with x-jquery-tmpl

c# - 带参数的表达式(LINQ、SQL、C#)

linq - Moq 测试 LINQ Where 查询