SharePoint/LINQ … Lookout for the gotchas!

Using SPMetal to query SharePoint objects is pretty cool… but a few recent experiences had me scratching my head.  Bottom line LINQ for SharePoint has come a long way, but I (personally) just don’t feel it’s quite there.

Here’s one example:

Got a column in a list of type “Choice”.  If you have a duplicate:

  • Choice 1
  • Choice 2
  • Choice 3
  • Choice 1
  • Choice 2
  • Choice 3
The Entity’s generated class will actually throw an error at run time:

System.ArgumentException: An item with the same key has already been added.

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Microsoft.SharePoint.Linq.Rules.ToEnumerableProcessor.GetEnumerableOperator(MethodInfo qm)
at Microsoft.SharePoint.Linq.Rules.ToEnumerableProcessor.ConvertMethod(MethodCallExpression mce, Context ctx)
at Microsoft.SharePoint.Linq.Rules.ToEnumerableProcessor.<.cctor>b__13(MethodCallExpression mce, Context ctx)
at Microsoft.SharePoint.Linq.Rules.GuardedRule`4.<>c__DisplayClass3.<.ctor>b__1(TSourceBase src, TContext ctx)
at Microsoft.SharePoint.Linq.Rules.SwitchRule`3.Apply(TSource src, TContext ctx)
at Microsoft.SharePoint.Linq.Rules.ToEnumerableProcessor.Process(Expression e, List`1& assumptions)
at Microsoft.SharePoint.Linq.SPLinqProvider.Rewrite(Expression expression, List`1& assumptions)
at Microsoft.SharePoint.Linq.SPLinqProvider.RewriteAndCompile[T](Expression expression, List`1& assumptions)
at Microsoft.SharePoint.Linq.LinqQuery`1.GetEnumerator()
at Microsoft.SharePoint.Linq.LinqQuery`1.System.Collections.IEnumerable.GetEnumerator()

Apparently I am not the only one who has ran into this either…

The other thing that makes me hesitant to use it at this stage is that you have to write a custom script to generate the Entity class since you don’t know if changes were made the the list that might have broke the references you added to your SharePoint app… granted this could happen using SPSite/SPWeb etc as well, but at least it’s easier to find (in my opinion) then beneath the layers of Entity generated code.

Has anyone else ran into other SharePoint/LINQ gotchas that has been making life a little difficult for you as well?