使用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.WhereSelectEnumerableIterator2.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 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產品中發現了我的第一個真正的錯誤,或者(我覺得更有可能)我做錯了什麼?
插入你的代碼 – 2010-11-09 16:30:58
我會看看使用SPQuery對象,然後一旦你有了你的結果,使用LINQ進一步過濾 – Mauro 2010-11-09 16:46:17
要回答這兩個意見:a)如果這個基本查詢失敗,你真的認爲包含.Where子句的查詢可能是問題?如果是這樣,那麼我會張貼更大的東西,但我真的不認爲這是必要的。 b)我寧願避免SPQuery;我使用LINQ的全部原因是爲了避免編寫那些惡意的CAML文件。 ;) – Shaamaan 2010-11-10 08:09:22