0

我有以下内容:

public void ProcessRequest(HttpContext context)
{
    string query = context.Request.QueryString["term"];
    System.Web.Script.Serialization.JavaScriptSerializer JsonSerializer =
        new System.Web.Script.Serialization.JavaScriptSerializer();

    List<Category> Categs = Category.getAll();
    var result = from c in Categs where Categs.Contains(c.Name) select c;
    context.Response.ContentType = "application/json";
    context.Response.Write(JsonSerializer.Serialize(result));
}

尝试为 jQuery UI 自动完成返回 [ { label: "Choice1", value: "value1" }, ... ] 列表。我有具有 ID 和 Name 属性的类别,我想根据查询字符串和 Name 属性中的“术语”过滤列表。我该怎么做?

提前致谢。

4

2 回答 2

2

如果您的最终目标是列出该列表[ { label: "Choice1", value: "value1" }, ... ]并按名称排序,那么这应该有效:

result
  .OrderBy(x => x.Name)
  .Select(x => new { label: x.Name, value: x.Id }); // create an anonymous type

编辑:您当前的查询:

var result = from c in Categs where Categs.Contains(c.Name) select c;

看起来不对。我认为该查询与仅选择 all 相同Categs。如果您想根据他们发送的术语进行查询,请在该 linq 表达式中替换c.Name为:query

var result = from c in Categs where Categs.Contains(query) select c;
于 2012-07-08T16:18:12.480 回答
0
string upperTerm = term.ToUpper();

var result = Category.getAll()
  .Where(c => c.Name.ToUpper().Contains(upperTerm))
  .Select(c => new { label = c.Name, value = c.Id });

然后 JSON 序列化result

于 2012-07-08T16:19:27.063 回答