Solved

Sharepoint 2010 LINQ InvalidCastException on Custom List

Posted on 2011-03-08
4
944 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

685 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