2014-07-27 56 views
2

我在想如何在早期綁定中使用LINQ獲得選項集字段的標籤CRM 2011在LINQ中獲取選項集標籤

我試過使用FormattedValues["field_name"]但我得到了以下錯誤The given key was not present in the dictionary

var query = (from username in context.SystemUserSet 
      select new { username.FullName, 
          Manager = (username.ParentSystemUserId == null) ? string.Empty : username.ParentSystemUserId.Name, 
          DepartmentValue = username.new_Department, 
          Department = username.FormattedValues["new_Department"] , 
          username.SystemUserId }); 

在此先感謝。

+0

小寫字母是否解決了您的問題? – Daryl

+0

不,我仍然有相同的錯誤信息 – user3881480

+0

您是否檢查過FormattedValues集合中的哪些字段?我只用它與查詢表達式,所以你可以嘗試新建一個新的SystemUser,而不是創建一個新的類型,並看看你是否可以訪問它的價值。 – Daryl

回答

0

你必須爲了選擇選項集字段設置爲能夠訪問格式化值:

var query = (from username in context.SystemUserSet 
      select new { username.FullName, 
          username.filed_name 
          Manager = (username.ParentSystemUserId == null) ? string.Empty : username.ParentSystemUserId.Name, 
          DepartmentValue = username.new_Department, 
          Department = username.FormattedValues["new_Department"] , 
          username.SystemUserId 
          }) 

更新

你已經選擇了它沒有看到(你拼錯場) 。

始終使用小寫字母。那是你的問題。

Department = username.FormattedValues["new_department"] 
+0

這並非總是如此,創建自定義字段時,您可以在'SchemaName'中使用混合大小寫,並且在編程中使用'SchemaName',並區分大小寫。 LogicalName將始終被強制爲小寫,但不被'Entity()'類用於屬性。這就是爲什麼我總是要求我的解決方案使用全部小寫'SchemaName'的值,以避免任何潛在的套管問題。 – Nicknow

+0

@Nicknow,邏輯名總是由屬性集合中的後期綁定實體和FormattedValues集合使用,所以這就是爲什麼我總是使用小寫(在C#後期綁定中,oData不同)。 – Daryl