将IList <int>强制转换为IList <string> </string> </int>

时间:2014-05-25 06:48:43

标签: c# asp.net-mvc casting controller converter

我的EFService中的

代码:

    private IDbSet<Item> _Items;
    private int _SearchTakeCount = 10;
    public IList<string> SearchByArticleCount(int articleCount)
        {
            return _Items.AsNoTracking().Where(m => m.Articles.Count().Equals(articleCount))
                .Take(_SearchTakeCount)
                .Select(m => m.Articles.Count().ToString())
                .Distinct()
                .ToList();
        }
我的控制器中的

代码:

public virtual ActionResult AutoCompleteSearch(string term, KeywordSearchBy searchBy = KeywordSearchBy.Name)
        {
            IList<string> data = new List<string>();
        switch (searchBy)
        {
            case ItemSearchBy.Name:
                data = _ItemService.SearchByName(term);
                break;
            case ItemSearchBy.ArticleCount:
                int articleCount = Convert.ToInt32(term);
                data = _ItemService.SearchByArticleCount(articleCount);
                break;
        }

当我运行项目时,异常得到了证实。 这个例外是: LINQ to Entities无法识别方法&#39; System.String ToString()&#39;方法,并且此方法无法转换为商店表达式。

1 个答案:

答案 0 :(得分:5)

在EF部分完成后,将ToString()调用为LINQ to Objects查询:

public IList<string> SearchByArticleCount(int articleCount)
{
    return _Items.AsNoTracking().Where(m => m.Articles.Count().Equals(articleCount))
        .Take(_SearchTakeCount)
        .Select(m => m.Articles.Count())
        .Distinct()
        .AsEnumerable()
        .Select(x => x.ToString())
        .ToList();
}