I'm working on a project which has localized site definitions. Now for one part, I wanted to use spmetal to generate a datacontext. Works, but I noticed the generated class retrieves lists by their name. I don't really understand why Microsoft would have picked this approach, since even a normal site admin can easily change the name of a list and thereby break the Linq class (or I'm missing something)?

I'd like to get the list by url and use that to query and insert items. Is this possible or do I have to revert back to CAML?

2 Answers 2


Found a solution, but I'm not sure it's the best one. So if someone has anything better, please feel free.

Instead of

var test = context.ListName.Where(l => l.Title.Contains("test")).SingleOrDefault()

First get the SPList and then perform:

var test = context.GetList<Item>(list.Title)
     .Where(l => l.Title.Contains("test")).SingleOrDefault()

By using the correct (localized) title it should work.

SharePoint: SharePoint to Linq pitfall

  • +1 it's solution. It is 'ugly' one but as far as I know - it is the only one. Personally I don't like fact that I need to get SPList object twice to preform some 'easy' task so I am still using CAML. Note: I formatted your answer to make it more readable. Commented Jan 20, 2012 at 0:44

I localize all my solution (so my feature receivers etc. are completely generic) and agree you should never query by some hardcode value. But why do a Contains? I use a ResourceHelper to get the list title directly from the resource files based on the used SPWeb's Locale.LCID. It's a wrapper around SPUtility.GetLocalizedString.

  • As I see it he is using Contains to get all list items with "test" in Title field. Problem is how to get list name if list Display name is localized and you are usint Linq so he is getting list by url and GetList. Can you look at my answer here sharepoint.stackexchange.com/questions/7597/… and see if you can improve it? Also providing some basic code example of your method would be nice. Commented Jan 20, 2012 at 1:45
  • See my answer in the question you linked to :-D!
    – Colin
    Commented Jan 21, 2012 at 0:42

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.