Solved

Sharepoint 2010 LINQ InvalidCastException on Custom List

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now