Solved

Sharepoint 2010 LINQ InvalidCastException on Custom List

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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