0

我正在使用 Visual Studio 2010、C# 和 Entity Framework 5。我正在生成一个 JSON 结构,它是 LINQ 查询的结果。在控制器中,我有以下内容:

[HttpPost]
public ActionResult ICD10ConditionSearch(string ICD10SearchTerm)
{
    CommonCodeEntities dataContextCommonCodes = new CommonCodeEntities(ConnectionString);

    IQueryable<ICD10Codes> codes = dataContextCommonCodes.ICD10Codes.
        Where(m => m.ICD10CodeTitle.Contains(ICD10SearchTerm));
    return Json(codes);
}

这可以正常工作并返回预期结果。

真正想做的是使用 lst 搜索词并连接结果。当我使用以下内容时:

[HttpPost]
public ActionResult ICD10ConditionSearch(string ICD10SearchTerms)
{
    String[] terms = ICD10SearchTerms.Split(' ');
    IQueryable<ICD10Codes> codes = Enumerable.Empty<ICD10Codes>().AsQueryable();
    IQueryable<ICD10Codes> codeLocal;            

    CommonCodeEntities dataContextCommonCodes = new CommonCodeEntities(ConnectionString);

    foreach (var term in terms)
    {
        codeLocal = dataContextCommonCodes.ICD10Codes.Where(m => m.ICD10CodeTitle.Contains(term));
        codes = codes.Concat(codeLocal);
    }
    return Json(codes);
}

这会产生以下错误This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code。我已经尝试过 Concat() 的其他变体,结果相同。

4

1 回答 1

1

删除 foreach 并尝试以下操作:

[HttpPost]
public ActionResult ICD10ConditionSearch(string ICD10SearchTerms)
{
    String[] terms = ICD10SearchTerms.Split(' ');
    CommonCodeEntities dataContextCommonCodes = new CommonCodeEntities(ConnectionString);
    IQueryable<ICD10Codes> codes = dataContextCommonCodes.ICD10Codes
       .Where(e => terms.Any(k => e.ICD10CodeTitle.Contains(k)).AsQueryable();

    return Json(codes);
}

为什么你不使用List<ICD10Codes>instate of IQueryable<ICD10Codes>

于 2013-02-07T00:11:02.053 回答