?
Solved

Sharepoint 2010 LINQ InvalidCastException on Custom List

Posted on 2011-03-08
4
Medium Priority
?
949 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 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