0

I am trying to get the items from a Sharepoint list with no luck. My code resembles the following:

  using (DataContext data = new DataContext(SPContext.Current.Web.Url))
  {
       var typeList= data.GetList<MyType>("SomeList");

       var ty = from t in typeList
                orderby t.SortOrder
                select t;

        //this causes the exception
        var aList = ty.ToList();
  }

Whenever I try to iterate over the items in a loop, or call ToList() on the result of the query, I get this exception:

An exception of type 'System.NullReferenceException' occurred in Microsoft.SharePoint.Linq.dll but was not handled in user code Additional information: Object reference not set to an instance of an object. occurred

I'm at a loss. In .Net, I can go things like this:

var something = myList.OrderBy(m => m.SortOrder).ToList();

How do I do something similar in Sharepoint?

1 Answer 1

0

Can you try this

using (DataContext data = new DataContext(SPContext.Current.Web.Url))
{
   var typeList= data.GetList<SPListItem>("SomeList");

   List<SPListItem> ty = (from SPListItem tin in typeList.Items
                orderby tin.Id
                select tin).ToList();

    foreach (var oItem in ty)
    {
        Console.WriteLine(oItem.Title);
    }
}

How to work with SharePoint Linq

5
  • Nope. I still get the same exception on the ToList() call. I've been to the MSDN examples.
    – Matt M
    Aug 3, 2017 at 19:01
  • I did a small change instead of MyType I used SPListItem. And I believe "SomeList" is your list title. Can you try this change Aug 3, 2017 at 19:03
  • That won't work because SPListItem doesn't have a SortOrder property.
    – Matt M
    Aug 3, 2017 at 19:06
  • Can you use ID in that place? like orderby tin.ID Aug 3, 2017 at 19:11
  • I can remove the orderby completely and still get the same exception.
    – Matt M
    Aug 3, 2017 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.