C# 无法识别方法 'System.String ToString()' 方法,并且该方法无法转换为存储表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12045176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:12:42  来源:igfitidea点击:

Not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

c#entity-frameworkentity-framework-4linq-to-entities

提问by Tripping

I have looked at all the examples related to this but have not been able to solve my issue.

我已经查看了与此相关的所有示例,但无法解决我的问题。

I am creating a dropdownlist in asp .net mvc3.

我正在 asp .net mvc3 中创建一个下拉列表。

I have a repository which returns:

我有一个返回的存储库:

    public IEnumerable<SelectListItem> GetPropertyTypeSelectList()
    {
        var propertyTypes = from p in db.PropertyType
                                orderby p.PropertyTypeDescription
                                select new SelectListItem
                                {
                                    Text = p.PropertyTypeDescription,
                                    Value = p.PropertyTypeId.ToString()
                                };
        return propertyTypes;
    }

My viewmodel looks like this:

我的视图模型如下所示:

public class AddPropertyViewModel
{
    public Property Property { get; set; }
    public IEnumerable<SelectListItem> PropertyTypes { get; set; }
    public IEnumerable<SelectListItem> FurnishedTypes { get; set; }
}

My controller for "create" action for HttpGet looks like this:

我用于 HttpGet 的“创建”操作的控制器如下所示:

    public ActionResult AddProperty()
    {
        AddPropertyViewModel viewModel = new AddPropertyViewModel
        {
            PropertyTypes = websiterepository.GetPropertyTypeSelectList()

        };
        return View(viewModel);
    }

and the view is like this:

视图是这样的:

    <div class="editor-label">
        @Html.LabelFor(model => model.Property.PropertyType)
        @Html.DropDownListFor(model => model.Property.PropertyType, Model.PropertyTypes)
    </div>

I am getting the error above. From what I have read it looks like ToString() is causing the problem. But I am not sure how to correct it.

我收到上面的错误。从我读过的内容来看,似乎 ToString() 是导致问题的原因。但我不确定如何纠正它。

Thanks.

谢谢。

采纳答案by Darin Dimitrov

LINQ to SQL doesn't know how to translate the .ToString()call to a SQL expression.

LINQ to SQL 不知道如何将.ToString()调用转换为 SQL 表达式。

So replace:

所以替换:

var propertyTypes = 
    from p in db.PropertyType
    orderby p.PropertyTypeDescription
    select new SelectListItem
    {
        Text = p.PropertyTypeDescription,
        Value = p.PropertyTypeId.ToString()
    };

with:

和:

var propertyTypes = db
    .PropertyType
    .OrderBy(x => x.PropertyTypeDescription)
    .ToList()
    .Select(x => new SelectListItem
    {
        Text = p.PropertyTypeDescription,
        Value = p.PropertyTypeId.ToString()
    });

Notice the .ToList()call to eagerly execute the SQL query after building the expression up until the OrderByclause and then do the .ToString()on the client (LINQ to Objects instead of LINQ to Entities) where the .ToString()expression is perfectly supported.

请注意.ToList()在构建表达式直到OrderBy子句之后急切执行 SQL 查询的调用,然后.ToString().ToString()完全支持表达式的客户端(LINQ to Objects 而不是 LINQ to Entities)上执行。

So here basically we are constructing a SQL query up until the OrderBy clause (including) and then will call .ToList to eagerly execute this query and fetch the resultset on the client. Then we continue chaining with a .Select statement. But we are no longer doing any LINQ to Entities or SQL stuff. We are now doing LINQ to Objects because all the resultset is now in-memory. And doing .ToString in LINQ to Objects doesn't pose any challenge.

所以这里基本上我们正在构建一个 SQL 查询,直到 OrderBy 子句(包括),然后将调用 .ToList 急切地执行这个查询并在客户端获取结果集。然后我们继续使用 .Select 语句进行链接。但是我们不再做任何 LINQ to Entities 或 SQL 的东西。我们现在正在执行 LINQ to Objects,因为所有结果集现在都在内存中。在 LINQ to Objects 中执行 .ToString 不会带来任何挑战。

Another possibility is to use the SqlFunctions.StringConvertbuilt-in function that knows how to translate it to SQL. This way you are keeping it lazy:

另一种可能性是使用SqlFunctions.StringConvert知道如何将其转换为 SQL的内置函数。这样你就可以保持懒惰:

var propertyTypes = 
    from p in db.PropertyType
    orderby p.PropertyTypeDescription
    select new SelectListItem
    {
        Text = p.PropertyTypeDescription,
        Value = SqlFunctions.StringConvert((double)p.PropertyTypeId)
    };