I have 2 lists, one list contains a lookup to another.

I have used SPMetal to generate entity.cs based on this article: http://www.eggheadcafe.com/tutorials/aspnet/b0c1cd0d-fe82-444e-a16e-7d3fb7d38eca/join-lists-with-linq--sh.aspx

I don't think I need to go the object LINQ route, as in the article because I have a lookup field on which I want to base the join.

I've gotten this far:

EntityDataContext site = new EntityDataContext(this.SiteURL);
site.ObjectTrackingEnabled = false;

EntityList<ListA> listAItems = site.GetList<ListA>(ListAName);
EntityList<ListB> listBItems = site.GetList<ListB>(ListBName);

This works so far and gives 2 flat EntityLists for both Sharepoint Lists.

From this point on, how would I create a query to just join, based on the lookup field?


If I was doing it in SQL, this is the statement I would be writing:

Select [fields] from table1 AA inner join table2 BB on AA.lookupfield = AA.Title/ID where BB.field = $inputFromUser

At the end of blog post you can find how to join and query two lists.

Hope it'll help.

  • That's not a join, that is a select with a where clause. I am talking about a select from table A inner join table B on A.pkid = B.fkid. Something like this.... – JL01 Apr 11 '11 at 15:18
  • Found this solution that has some great examples: solutionizing.net/2009/04/30/join-sharepoint-lists-with-linq – JL01 Apr 11 '11 at 18:08
  • Well it's ok if your lists are small, but you can get performance hit if they are large, beacuse you retreaving all items from both lists and then doing join - not the best way, better think about using lookup field – Andriy Shvay Apr 12 '11 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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