0

Using SPMETAL I've generated a C# file allowing me the use of LINQ to access Sharepoint objects.

However, using a very simple query produces a strange error:

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.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 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()

The query itself looks like this:

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

Granted, this is not the most optimal query, but one which uses .Where to filter only the needed records also returns the same error.

I found I can select specific columns using .Select(z => new ZlecenieItem(){ ColumnName = z.ColumnName, ... }), however I cannot use this to select lookup columns, as I get the following error then (even if I select only a single column this way):

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

This is problematic because sometimes I need to get values from other lists, and it would be nice to use the lookup values.

Did I just find my first ever genuine bug in a MS product, or (what feels a lot more likely) am I doing something wrong?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
MBender
  • 5,395
  • 1
  • 42
  • 69
  • I'd look at using an SPQuery object instead, then once you have your result from that, use LINQ to filter it further – Mauro Nov 09 '10 at 16:46
  • To answer both comments: a) if this basic query fails, do you really think a query containing a .Where clause could be the issue? If so, then I will post the bigger thing, but I honestly don't think it's needed. b) I'd rather avoid SPQuery; the whole reason I'm using LINQ is to avoid writing those darned CAML files. ;) – MBender Nov 10 '10 at 08:09

1 Answers1

1

While I'm still unsure why the above didn't work, I managed to solve the problem by adding a custom configuration file to SPmetal to generate the reference with.

This would suggest that the default SPmetal configuration might not always work.

MBender
  • 5,395
  • 1
  • 42
  • 69
  • Would you happen to remember what settings you needed to put in the configuration file? All of a sudden all my metal objects stopped working and this is the exact error I'm getting. – CMN Jan 13 '11 at 00:09
  • I've forced SPMetal to generate only classes for those SP lists that I actually need, and then I limited those classes to contain only the most basic properties. As such any properties used I didn't need in my code (which are used by SP workflows or which are references to other lists) were omitted. – MBender Jun 14 '11 at 13:30