所以我有以下查询
IQueryable<Feedback> query = db.Feedbacks;
var result = query
.Select(s => new FeedbackViewModel
{
businessId = s.Account.Businesses.FirstOrDefault().businessId,
hireSuccess = s.hireSuccess.Value,
useAgain = s.useAgain.Value,
suggestions = s.suggestions,
employees = numberGroups[s.numEmployees.Value]
});
return result;
即时收到错误
其他信息:LINQ to Entities无法识别该方法 &#39; System.String get_Item(Int32)&#39;方法,这个方法不能 翻译成商店表达。
由于尝试从字典查找中设置employees字符串,因此字典看起来像这样。
public static Dictionary<int, string> numberGroups = new Dictionary<int, string>()
{
{ 0, "None" },
{ 9, "1 to 9" },
{ 19, "10 to 19" },
{ 49, "20 to 49" },
{ 100, "50 to 100" }
};
FeedbackViewModel看起来像这样
public class FeedbackViewModel
{
public int businessId { get; set; }
public bool hireSuccess { get; set; }
public bool useAgain { get; set; }
public string suggestions { get; set; }
public string employees { get; set; }
}
我做错了什么?
答案 0 :(得分:1)
我认为您正在尝试从数据库中查询某些内容。 Linq To SQL不支持numberGroups[s.numEmployees.Value]
等方法。
您可以考虑做这样的事情。
var query = db.Feedbacks.Select(f = > new
{
businessId = s.Account.Businesses.FirstOrDefault().businessId,
hireSuccess = s.hireSuccess.Value,
useAgain = s.useAgain.Value,
suggestions = s.suggestions.Value,
numEmployees = s.numEmployees.Value
}).ToList();
var result = query
.Select(s => new FeedbackViewModel
{
businessId = s.businessId,
hireSuccess = s.hireSuccess,
useAgain = s.useAgain,
suggestions = s.suggestions,
employees = numberGroups[s.numEmployees]
}).ToList();
答案 1 :(得分:0)
LINQ to Entities不支持Convert.ToInt32,这可能是你的一个* .Value调用隐式执行的操作。你需要先在LINQ之外解析为int。如果我能看到FeedbackViewModel,我可能会提供更多细节。