所以我有以下查询
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;
我得到了错误
Additional information: LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression.
由于试图从字典查找中设置员工字符串,字典看起来像这样。
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; }
}
我做错了什么?
最佳答案
我认为您正在尝试从数据库中查询某些内容。 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();
关于c# - 根据字典查找查询填充字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166424/