为什么LINQ to Entities无法识别方法'System.String ToString()

MVC3 Web应用程序内部出现错误。 LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.


当我尝试使用EF从查询中获取值时:


public class DataRepository

    {

        public mydataEntities1 dbContext = new mydataEntities1();


        public List<SelectListItem> GetPricingSecurityID()

        {

        var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing

                                     select new SelectListItem

                                         {

                                                Text = m.PricingSecurityID.ToString(),

                                                Value = m.PricingSecurityID.ToString()

                                         });


        return pricingSecurityID.ToList();

        }

    }


江户川乱折腾
浏览 1716回答 3
3回答

繁星点点滴滴

那不能转换成SQL。我猜,从理论上讲,它可以,但是没有实现。得到结果后,只需执行投影即可:var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select m.PricingSecurityID).AsEnumerable()&nbsp; &nbsp; .Select(x => new SelectListItem{ Text = x.ToString(), Value = x.ToString() });

杨__羊羊

这个怎么样。在此示例中,db中的VDN字段和Skill字段都是整数。我正在寻找来自两个领域的比赛,所以我有2个比较。包括以下内容:using System.Data.Objects.SqlClient; // needed to convert numbers to strings for linq比较数字时,请执行以下操作:&nbsp; &nbsp; &nbsp; &nbsp; // Search Code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!String.IsNullOrEmpty(searchString))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; depts = depts.Where(d => SqlFunctions.StringConvert((double)d.VDN).Contains(searchString.ToUpper())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || SqlFunctions.StringConvert((double)d.Skill).Contains(searchString.ToUpper()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // End Search Code工夫。
打开App,查看更多内容
随时随地看视频慕课网APP