0

使用SPMETAL我生成了一個C#文件,允許我使用LINQ訪問Sharepoint對象。指定的轉換無效查詢Sharepoint

然而,使用非常簡單的查詢產生一個奇怪的錯誤:

Specified cast is not valid.

Stack trace:
at Set__zleceniaTrigger(Object , Object) at Microsoft.SharePoint.Linq.StaticPropertyMap.SetToEntity(Object entity, Object value) at Microsoft.SharePoint.Linq.SPItemMappingInfo.MaterializeEntity[TEntity](DataContext dc, SPDataList list, SPListItem item, SPItemMappingInfo itemMappingInfo, JoinPath joinPath) at lambda_method(ExecutionScope , SPListItem) at System.Linq.Enumerable.WhereSelectEnumerableIterator 2.MoveNext() at System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable 1 source) at QuickModify.Program.Main(String[] args) in C:\XXXXXXXXX\QuickModify\Program.cs:line 42 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

查詢本身是這樣的:

SPEntitiesDataContext ctx = new SPEntitiesDataContext("http://localhost:1080"); 
var tasks = ctx.Zlecenia.ToList(); 

當然,這還不是最優化的查詢,但其中一個在只過濾所需記錄的地方也會返回相同的錯誤。

我發現我可以用.Select(z => new ZlecenieItem(){ ColumnName = z.ColumnName, ... })選擇特定的列,不過我不能用它來選擇查找列,我收到以下錯誤,那麼(即使我只選擇一列這樣):

The query uses unsupported elements, such as references to more than one list, or the projection of a complete entity by using EntityRef/EntitySet.

這是有問題的,因爲有時我需要從其他列表中獲取值,並且使用查找值會很好。

我剛剛在MS產品中發現了我的第一個真正的錯誤,或者(我覺得更有可能)我做錯了什麼?

+0

插入你的代碼 – 2010-11-09 16:30:58

+0

我會看看使用SPQuery對象,然後一旦你有了你的結果,使用LINQ進一步過濾 – Mauro 2010-11-09 16:46:17

+0

要回答這兩個意見:a)如果這個基本查詢失敗,你真的認爲包含.Where子句的查詢可能是問題?如果是這樣,那麼我會張貼更大的東西,但我真的不認爲這是必要的。 b)我寧願避免SPQuery;我使用LINQ的全部原因是爲了避免編寫那些惡意的CAML文件。 ;) – Shaamaan 2010-11-10 08:09:22

回答

1

儘管我仍然不確定上述原因,但我仍然設法通過向SPmetal添加一個自定義配置文件來解決問題,以生成引用。

這表明默認的SPmetal配置可能無法正常工作。

+0

你會記得你需要把什麼設置放在配置文件中嗎?突然間,我所有的金屬物體都停止工作,這是我得到的確切錯誤。 – CMN 2011-01-13 00:09:23

+0

我強制SPMetal只爲那些實際需要的SP列表生成類,然後限制這些類只包含最基本的屬性。因爲我在代碼中不需要使用的任何屬性(由SP工作流程使用,或者是對其他列表的引用),因此省略。 – Shaamaan 2011-06-14 13:30:00

相關問題