cast IList<int> to IList<string>

本文关键字:gt lt IList string to int cast | 更新日期: 2023-09-27 18:04:53

code in my 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();
        }

code in my Controller:

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到实体不能识别"System"方法。

cast IList<int> to IList<string>

String ToString()方法,该方法不能转换为存储表达式。

在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();
}