Solved

Sharepoint 2010 LINQ InvalidCastException on Custom List

Posted on 2011-03-08
4
942 Views
Last Modified: 2012-05-11
I'm having some problem running a LINQ query against custom SharePoint lists.

Using what I've learned from my training manuals, I used SPMETAL to write an entity model class, which I have imported into my project. Then I write the following code

   
    using(myEntityModelDataContext dataContext = 
                new myEntityModelDataContext("http://sharepoint/mysite"))
                {
                    var listData = from data in dataContext.myList select data;
                    Console.WriteLine(listData.Count());

                    foreach (var listItem in listData)
                    {
                        Console.WriteLine(listItem .Title);
                    }
                }

Open in new window


The error occurs at the "foreach" line on the "listItem" variable.

The Count() method returns the correct number of rows in the list, so I know that is working. And if I replace "myList" with a native SharePoint list in that site (e.g. Tasks), this works fine.

My lists were created in SharePoint Designer, and I don't know if that has anything to do with it.

Thanks,
Rob
0
Comment
Question by:LTIADMIN
  • 3
4 Comments
 

Author Comment

by:LTIADMIN
ID: 35075607
I have learned that this is related to the lists having workflows attached to them. If I remove the workflows, the code works fine.

But I need those workflows. Anyone know how to do this with a list that has a workflow?

0
 
LVL 5

Expert Comment

by:navtej_dhillon
ID: 35081214
Can you try using CAML query instead?

I believe even LINQ uses CAML under the hood of extracting/filtering data, so instead i would recommend use CAML.

~Amit
0
 

Accepted Solution

by:
LTIADMIN earned 0 total points
ID: 35082707
I fixed it. If I go into the file created by SPMetal, I can find columns in these lists related to the workflow. If I comment these out, the LINQ query works fine.

That's one solution. Microsoft's guidance on this suggests ways of creating this entity data without using SPMetal, and I might try that too.

Thanks.
0
 

Author Closing Comment

by:LTIADMIN
ID: 35126229
Solved it myself.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question