简体   繁体   English

转换IList <int> 到IList <string>

[英]cast IList<int> to IList<string>

code in my EFService: 我的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;
        }

when I run the project the exception is accured. 当我运行该项目时,会出现异常。 this exception is: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. 此异常是:LINQ to Entities无法识别方法'System.String ToString()',并且该方法无法转换为商店表达式。

Make ToString() call as LINQ to Objects query, after EF part is done: 在完成EF部分之后,以LINQ的形式对对象查询进行ToString()调用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM