2012-08-22 32 views
3

使用MVC3 VS2010和SQL Server 2008 Express 我试图基于两个SQL服务器表并显示结果。 一个表是客户表,另一个表是代理。它们在客户端表中具有相同的ClientAgentID,在代理表中具有相同的ID。代理程序日志,应该能够看到分配给代理程序的客户端。如果你有任何想法来做到这一点,请帮助我。到目前为止,我正在尝试在客户端控制器中进行筛选,这里是我所拥有的,但是我得到的信息是标题中的。LINQ to Entities不能识别方法'System.String ToString(Int32)'方法,并且此方法不能转换为存储表达式

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()); 
    } 

这是我第一个音乐商店之后的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()); 
    } 
+0

[LINQ to Entities可能重复无法识别方法'Int32](http://stackoverflow.com/questions/3316615/linq-to-entities-does-not-recognize-the-method-int32) –

+0

AgentsPkg和MVCInternetApplicationPkg之间是否定义了外键? – Merenzo

+0

@Merenzo在SQL数据库中定义了一个外键,但在MVC中没有引用外键的地方 – WillNZ

回答

2

1.错误原因:

正如其他人指出,这是由于您的where子句中使用Convert.ToString(),它的Linq不能转换成SQL。我希望你的原始查询只需要删除两个Convert.ToString()函数。

2. “要做到这一点....最好的方法”:

好,更好的办法.... :)

在实体框架,最简单的方式有关之间导航实体是通过Navigation Properties。如果你的方法是“数据库优先”,这些应该在你的EDMX中为你生成。如果你的方法是“Code First”,那么有一个很好的帖子here描述了如何设置它。

无论哪种方式,我期待你的Client类有(你提到即类似的OrderDetail年代MvcMusicStore样品中Order属性)导航属性Agent

public virtual Agents Agent { get; set; } 

然后你的方法变得非常简单(即类似于MvcMusicStore中的许多控制器方法)...不需要Joins或多个语句:

var clients = db.MVCInternetApplicationPkg.Where(c => c.Agent.AgentLogin == User.Identity.Name); 
return View(clients.ToList()); 
+0

感谢这个例子,我会试一试,让你知道我是怎么开始的。 – WillNZ

+0

将此标记为答案的原因是由于@Merenzo的支持和他在答案中的推理。如果你用代码回答,但没有解释,任何人如何学习? – WillNZ

3

你不想使用转换在您的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); 
+0

感谢这个例子,我会试一试,让你知道我如何继续。 – WillNZ

1

这是怎么回事是LINQ提供程序(LINQ到实体)正在试图查询转换为SQL,并没有用于转换没有映射。这些错误有时难以破译,但线索在“String.ToString()”行中。还认识到,由于延迟执行的,错误将不会出现,直到clientItems进行迭代,你的情况与调用toList()return View(clientItems.ToList());

1

的答案是使用SqlFunctions.StringConvert(使用“小数”或“双”,但“诠释”将无法正常工作)见例如

using System.Data.Objects.SqlClient ; 

var clientItems = from r in db.MVCInternetApplicationPkg 
         where SqlFunctions.StringConvert((double), r.ClientAgentID) 
          == SqlFunctions.StringConvert((decimal) , getAgentID) 
         select r; 

更多信息请参阅http://msdn.microsoft.com/en-us/library/dd466166.aspx

+0

这是解决我的问题的答案。 – guneysus

相关问题