在以下情况下,我经常遇到一个错误,例如"无法从'方法组'转换为'字符串'"
var list = new List<string>();
// ... snip
list.Add(someObject.ToString);
Run Code Online (Sandbox Code Playgroud)
当然最后一行有一个拼写错误,因为我忘记了之后的调用括号ToString.正确的形式是:
var list = new List<string>();
// ... snip
list.Add(someObject.ToString()); // <- notice the parentheses
Run Code Online (Sandbox Code Playgroud)
我正在将一些东西从一个mysql服务器迁移到一个sql server但我无法弄清楚如何使这个代码工作:
using (var context = new Context())
{
...
foreach (var item in collection)
{
IQueryable<entity> pages = from p in context.pages
where p.Serial == item.Key.ToString()
select p;
foreach (var page in pages)
{
DataManager.AddPageToDocument(page, item.Value);
}
}
Console.WriteLine("Done!");
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
当它进入第二个时foreach (var page in pages)它抛出一个异常说:
LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式.
谁知道为什么会这样?
如何将int和int转换为字符串?以下都不起作用:
from s in ctx.Services
where s.Code.ToString().StartsWith("1")
select s
from s in ctx.Services
where Convert.ToString(s.Code).StartsWith("1")
select s
from s in ctx.Services
where ((string)s.Code).ToString().StartsWith("1")
select s
Run Code Online (Sandbox Code Playgroud)
编辑
我得到的错误是:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
在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();
}
}
Run Code Online (Sandbox Code Playgroud)