A: 

Not sure there is a solution to this problem with Linq to Sql. If you are using Sql Server 2005 you could define a (recursive like) Stored Procecdure that uses common table expressions to get the result that you want and then execute that using DataContext.ExecuteQuery.

Simon Fox
+2  A: 

Maybe you could try taking a step back and seeing what you want to do with the relation? I'm assuming you want to display this information to the user in e.g. "modified by Iain Galloway 8 hours ago".

Could something like the following work? :-

var users = from u in db.Users
            select new
            {
              /* other stuff... */
              AddedTimestamp = u.AddedTimestamp,
              AddedDescription = u.AddedByUser.FullName,
              ChangedTimestamp = u.ChangedTimestamp,
              ChangedDescription = u.ChangedByUser.FullName
            };

I've used an anonymous type there for (imo) clarity. You could add those properties to your User type if you preferred.

As for your second question, your normal LoadWith(x => x.AddedByUser) etc. should work just fine - although I tend to prefer storing the description string directly in the database - you've got a trade-off between your description updating when ChangedByUser.FullName changes and having to do something complicated and possibly counterintuitive if the ChangedByUser gets deleted (e.g. ON DELETE CASCADE, or dealing with a null ChangedByUser in your code).

Iain Galloway
Yes I do want to display that info in my GUI. I was hoping to solve this problem without the use of anonymous types. But i guess it IS one way way of solving the problem. Is my problem related to the Linq to SQL's inability to cope with many to many relationships?
Saajid Ismail
Na, you could add User.AddedDescription and User.ChangedDescription fields to the User class and then you wouldn't have to use an anon type.It's not really anything to do with the many-many relationship (each relation in your example is 1:1).If you were writing your SQL directly, you wouldn't want to get all the properties of the AddedByUser (including his AddedByUser and so on into infinity). You'd probably just grab his name and/or whatever details you needed for your view.Imo it's a more general O/RM problem. I think you'd have the same issue with L2E or Hibernate.
Iain Galloway
+1  A: 

Any type of cycles just aren't allowed. Since the LoadWith<T> or AssociateWith<T> are applied to every type on the context, there's no internal way to prevent an endless loop. More accurately, it's just confused on how to create the SQL since SQL Server doesn't have CONNECT BY and CTEs are really past what Linq can generate automatically with the provided framework.

The best option available to you is to manually do the 1 level join down to the user table for both of the children and an anonymous type to return them. Sorry it's not a clean/easy solution, but it's really all that's available thus far with Linq.

Nick Craver