This is easy to do in SQL and I'm having a very hard time achieving this using Linq to SQL. I have two entities setup, Project and ProjectsbyUser:
[Table(Name = "Projects")]
public class Project
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, Name="Job")]
public string ProjectId { get; set; }
[Column(Name = "Description")]
public string Description { get; set; }
[Association(OtherKey = "ProjectId")]
private EntitySet<ProjectList> _projectlist = new EntitySet<ProjectList>();
public IEnumerable<ProjectList> ProjectList { get { return _projectlist; } }
}
[Table(Name = "ProjectLists")]
public class ProjectList
{
[Column(IsPrimaryKey = true)]
public string UserId { get; set; }
[Column(IsPrimaryKey = true)]
public string ProjectId { get; set; }
}
So in my ProjectsController I'll have this:
public ViewResult myProject()
{
var query = projectsRepository.Projects
.Where(x => x.ProjectId == "H1000").ToList();
return View(query.ToList());
}
And hey, what do you know, it will show all the users attached to project H1000 with a couple of nice nested foreach statements inside my view. Easy as can be, but I'm trying to filter based on user. I can get the user easy enough (User.Identity.Name.ToString() which matches the UserId column). This has to be easy to do but I'm not figuring out a way to accomplish it without writing terrible code. This blog post explains what I want to do but it appears to use "Linq to SQL classes" template and right now my entities are sitting in nice C# class files and not in VS2008 fancy GUI designer files. There must be an elegant way to solve this, I've been working on this for about a week and think I might have run myself into a corner. Thanks for your help!