c# - LINQ to Entities 无法识别方法 'System.String ToString(Int32)' 方法,并且无法将此方法翻译成存储表达式

标签 c# linq entity-framework linq-to-entities

使用 MVC3 VS2010 和 SQL Server 2008 Express 我正在尝试基于两个 SQL Server 表进行过滤并显示结果。 一张表是客户表,另一张是代理。它们在客户表中有共同的 ClientAgentID,在 Agents 表中有 ID。代理登录并且应该能够看到分配给该代理的客户端。如果您对执行此操作的最佳方法有任何想法,请帮助我。到目前为止,我正在尝试在客户端 Controller 中进行过滤,这就是我所拥有的,但我得到的消息是在标题中。

 public ActionResult Index()
    {
        //This displays all the clients not filtered by the Agent ID number
        //var clientItems = db.MVCInternetApplicationPkg;
        //return View(clientItems.ToList());

        //Trying to filter by the agent name given in the login page then finding 
        //the agent ID

        var getAgentID = from a in db.AgentsPkg
                            where a.AgentLogin == User.Identity.Name
                            select a.ID;

        var clientItems = from r in db.MVCInternetApplicationPkg
                          where Convert.ToString(r.ClientAgentID)
                                == Convert.ToString(getAgentID)
                          select r;
        //THIS IS THE LINE OF CODE THAT SHOWS THE ERROR MESSAGE
        return View(clientItems.ToList());
    }

这是我在 Music Store 之后的第一个 MVC 项目,所以我愿意学习并接受任何帮助或建议。 干杯

这是我最后使用的解决方案。如果这是一个好方法,我们将不胜感激

 public ActionResult Index()
    {

        var innerJoint = from agents in db.AgentsPkg where agents.AgentLogin == User.Identity.Name
                         join clients in db.MVCInternetApplicationPkg on agents.ID equals clients.ClientAgentID
                         select clients;

        return View(innerJoint.ToList());
    }

最佳答案

您不想在您的 linq 语句中使用转换!!!!!!

 string clientagentid=Convert.ToString(r.ClientAgentID);
    string getagentid= Convert.ToString(getAgentID);

var clientItems = (from r in db.MVCInternetApplicationPkg
                          where clientagentid==getagentid
                          select r).ToList();
 return View(clientItems);

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

相关文章:

c# - .NET、.Contains() 或 .Count() 哪个更快?

c# - 如何在 EF 查询中将 Uint 转换为 Nullable Int

c# - Entity Framework 删除所有行而不是一行

c# - ASP.NET 3.5 GridView 行选择

c# - WebMethod 返回 JSON 对象问题

c# - 如何在不终止进程的情况下处理 System.Diagnostics.Process 对象?

.net - 查找键的索引?词典.NET

c# - 为什么 Entity Framework 中忽略 uint 枚举

c# - Entity Framework 核心 - DefaultValue(true) 属性不起作用

c# - 如何设计我的 C# jQuery API,使其使用起来不会混淆?