c# - 根据字典查找查询填充字符串

标签 c# asp.net entity-framework

所以我有以下查询

        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/

相关文章:

asp.net - 访问一个asp :hiddenfield control in JavaScript

javascript - "Undefined"有时出现在我的 ASP.NET 应用程序的 URL 中(不知何故与谷歌相关?)

asp.net - 隐藏 Kendo UI DatePicker 文本框

c# - 无法转换类型的对象

c# - 重构异步/等待并行处理

c# - Visual Studio 2013 - 源代码不可用

c# - EF AsNoTracking 导致错误。类型 'System.DateTime' 的表达式不能用于分配类型 'System.Nullable` 1[System.DateTime]'

c# - 此 LINQ JOIN 是否存在某种语法错误?

c# - EF4.1(代码优先)——如何指定复合关系

java - 如何使用java或C#计算执行时间