簡體   English   中英

LINQ to Entities無法識別方法'System.String ToString(Int32)'方法,並且此方法無法轉換為存儲表達式

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

使用MVC3 VS2010和SQL Server 2008 Express我試圖基於兩個SQL Server表進行篩選並顯示結果。 一個表是客戶表,另一個表是代理。 它們在clients表中具有共同的ClientAgentID,在Agents表中具有ID。 代理會記錄並應該能夠看到分配給代理的客戶端。 如果您對最佳方法有任何想法,請幫助我。 到目前為止,我試圖在客戶端控制器中進行過濾,這是我所擁有的,但我得到的消息是在標題中。

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

你不想在你的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);

1.錯誤原因:

正如其他人所說,這是由於在where子句中使用了Convert.ToString() ,而Linq無法將其轉換為SQL。 我希望您的原始查詢只需刪除兩個Convert.ToString()函數即可。

2.“......最好的方法”:

好吧,更好的方式.... :)

在實體框架中,在相關實體之間導航的簡便方法是通過導航屬性 如果您的方法是“數據庫優先”,則應在EDMX中為您生成這些方法。 如果您的方法是“Code First”,那么這里有一篇很好的帖子描述了如何設置它。

無論哪種方式,我希望您的Client類具有Agent的導航屬性(即類似於您提到的MvcMusicStore示例中的OrderDetail的Order屬性):

public virtual Agents Agent { get; set; }

然后你的方法變得非常簡單(即類似於MvcMusicStore中的許多控制器方法)......沒有Joins或需要多個語句:

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

答案是使用SqlFunctions.StringConvert (使用'decimal'或'double'但'int'不起作用)參見示例

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

發生了什么是Linq提供程序(Linq to Entities)正在嘗試將您的查詢轉換為SQL,並且沒有Convert的映射。 這些錯誤有時難以破譯,但線索在“String.ToString()”行中。 還要意識到,由於延遲執行,錯誤不會出現,直到clientItems被迭代,在你的情況下調用toList() return View(clientItems.ToList());

LINQ無法將Convert.ToInt32()映射到等效的T-SQL,因此它會拋出異常。 由於COLD TOLD表示您必須轉換該值,然后在查詢中使用轉換后的值。

請檢查: 為什么LINQ to Entities不識別某些方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM