2010-03-24 38 views
0

我有2個實體「UserProfile」和「Agent」,它們是1-many關係。我想通過提供userProfileEntityKey來執行查詢以獲取代理列表。當我運行它時,我得到了這個「LINQ to Entities不支持指定的類型成員'EntityKey'」錯誤。LINQ to Entities不支持指定的類型成員'EntityKey'

public IQueryable<Agent> GetAgentListByUserProfile(EntityKey userProfileEntityKey) 
{ 
ObjectQuery<Agent> agentObjects = this.DataContext.AgentSet; 

IQueryable<Agent> resultQuery = 
        (from p in agentObjects 
        where p.UserProfile.EntityKey == userProfileEntityKey 
        select p); 
    return resultQuery; 
} 

那麼,什麼是正確的方法來做到這一點?我是否使用p.UserProfile.UserId = UserId?如果是這樣的話,那就不再是概念了。或者我應該寫對象查詢,而不是LINQ查詢?

回答

0

嘗試......

首先,覆蓋在實體對象Equals()(和GetHashCode())方法。如果兩個實體對象是相同的類型並且它們的ID相同,則它們應該相等。

接下來,使用此操作

public IQueryable<Agent> GetAgentListByUserProfile(UserProfile userProfile) 
{ 
    var agentObjects = this.DataContext.AgentSet; 
    var results = (from p in agentObjects 
        where p.UserProfile == userProfile 
        select p); 
    return results; 
} 
1

我會去的easer路線p.UserProfile.UserId = UserId是您的解決方案。

這是假設UserId是您的表的主鍵。

通過EntityKey進行搜索並不能使其成爲概念。當你展示一個以不同方式呈現你的數據庫層的圖層時,這個概念性部分就會出現。

我誤解了你對概念的看法嗎?

相關問題