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

标签 c# asp.net-mvc linq entity-framework

我的 asp.net mvc 应用程序中有以下 View :-

 @model MvcApplication4.Models.ContactsDetails
     <h3>Select Contacts Detials</h3>
    <p class="dashboarder" style = "color:#5c87b2"><strong>@Model.Info.Count()</strong> Selected Customers Accounts.</p>
    @using (Html.BeginForm("Export", null))
    {
        Int32 c = -1;
        <table>
            <tr>
                <th>
                    Account Name @Html.CheckBox("IncludeAccountName", true)
                </th>
                <th>
                    Name @Html.CheckBox("IncludeName", true)
                </th>

使用以下操作方法填充:-

public ActionResult CustomersDetails(long[] SelectRight)
{

    if (SelectRight == null)
    {
        return RedirectToAction("customer", new { isError = true });
    }
    else
    {

        var ContactsDetails2 = new ContactsDetails
        {
            Info = r.getcontactinfo(SelectRight)
        };
        return View(ContactsDetails2);

    }          


}

然后是以下存储库方法:-

public IEnumerable<AaaUserContactInfo> getcontactinfo(long[] id)
{
    var organizationNames = entities.SDOrganizations
                   .Where(org => id.Contains(org.ORG_ID))
                   .Select(org => org.NAME);


    var result = from userContactInfo in entities.AaaUserContactInfoes
                 join contactInfo in entities.AaaContactInfoes on userContactInfo.CONTACTINFO_ID equals contactInfo.CONTACTINFO_ID
                 where organizationNames.Contains(contactInfo.EMAILID.ToString())
                 select userContactInfo;
    return result;

但是当我运行 mu 应用程序时,我在 View 上遇到了以下不清楚的错误:-

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

在以下代码上:-

<strong>@Model.Info.Count()</strong>

最佳答案

使用SqlFunctions.StringConvert方法而不是 ToString(无法转换为 SQL):

where organizationNames.Contains(SqlFunctions.StringConvert(contactInfo.EMAILID))

如果 EMAILID 不是 double 或十进制,则只需将值转换为其中一种类型。

关于c# - LINQ to Entities 无法识别方法 'System.String ToString()' 方法,并且该方法无法转换为存储表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14149736/

相关文章:

c# - 在 Linq 中查找正则表达式匹配的索引

c# - 如何在 Windows Phone 中禁用多点触控?

c# - WCF 模拟不是模拟管理员

css - ASP.NET HTML 布局

linq - 如何在 dbml 文件中动态创建类

.net - Entity Framework : Can't use "Contains" with property on another object

c# - 如何在 c# foreach 循环中使用 MongoDB 的 Query 和 QueryBuilder?

c# - 使用套接字获取对象处置异常

javascript - Bootstrap 部分 View 模式中的 MVC 谷歌地图不起作用

.net - 采用点网技术的 N 层架构