tags:

views:

49

answers:

1

Hi I'm wondering how I can go about this problem

I have a DB with a Users Table, and a Followers table. The Followers table contains 2 Columns "FollowerID" and "FollowedID" I have a 1 -> * relation in my datamodel between Users.ID -> Followers.FollowerID and Users.ID -> FollowedID

How do I in LINQ get a set of users that are following a specific user?

I'll express what I'm trying to achieve programatically I can get this far:

ctx.Followers.Where(f => f.FollowedID == CurrentUser.ID)

so now i have a Followers set where I have the ID of the users that follow the CurrentUser, and I could iterate through this collection and then add users after each iteration to a collection that would be a total USER collection that followed CurrentUser, but isn't there a smarter, or LINQ'er way to do this?

Much appreciated

Thx

A: 
ctx.Followers.Where(f => f.FollowedID == CurrentUser.UserId)
             .Select(f => f.FollowerUser).Distinct()

If you have an EntityRef for the FollowerID (example name FollewerUser) you can use this to get all users following a particular ID

Stephan
I see, but then how do I get an entityreference for a user? I thought, I'd do this with the relationship, but all I have is the ID
Jakob
Are you using LINQ-To-SQL or EntityFramework? With LINQ-To-SQL you need to setup a foreign key relationship in your DBML. I believe EF is similar but not really sure.
Stephan
I was using the EntityFramework, because of a Domain Service, but I think I'll just switch to LINQ-To-SQL as I know that better aswell
Jakob