By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,667 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,667 IT Pros & Developers. It's quick & easy.

Linq.How to complete this query?

P: n/a
Hello,

I have 3 tables with a many to many relationship:

Professors ProfessorID, Age, ...
Tags TagID, Name, ...
ProfessorsTags ProfessorID, TagID

How can I get all the professors and all tags associated to each
professor using LINQ?

I have the following:

List<ProfessorPaperpapers = (from p in database.Professors
orderby p.Name
select new ProfessorPaper {
Professor = p,
Tags = new List<Tag{

}
}
}).ToList();

ProfessorPaper is a wrapper that contains two properties: Professor
and List<Tag>.
In my query what I am missing is filling the tags for each professor.

Could someone, please, tell me how to do this?

Thanks,
Miguel
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 27, 1:15*am, shapper <mdmo...@gmail.comwrote:
Hello,

I have 3 tables with a many to many relationship:

Professors ProfessorID, Age, ...
Tags TagID, Name, ...
ProfessorsTags ProfessorID, TagID

How can I get all the professors and all tags associated to each
professor using LINQ?

I have the following:

* * * List<ProfessorPaperpapers = (from p in database.Professors
* * * * * * * * * * * * * * * * * * * * * orderby p.Name
* * * * * * * * * * * * * * * * * * * * * select new ProfessorPaper {
* * * * * * * * * * * * * * * * * * * * * * Professor = p,
* * * * * * * * * * * * * * * * * * * * * * Tags = new List<Tag{

* * * * * * * * * * * * * * * * * * * * * * }
* * * * * * * * * * * * * * * * * * * * * }
* * * * * * * * * * * * * * * * * * * * * }).ToList();

ProfessorPaper is a wrapper that contains two properties: Professor
and List<Tag>.
In my query what I am missing is filling the tags for each professor.

Could someone, please, tell me how to do this?
Well, you could try:

Tags = from pt in database.ProfessorsTags where pt.Professor == p
select pt.Tag

I don't know how nicely that will translate into SQL though.

Alternatives will depend on how you've mapped the ProfessorsTags
collection. For instance, if you've got two many-to-one relationships
mapped, and you've made both of them bidirectional, you may be able to
do:

Tags = p.ProfessorsTags.Tags

Again, I don't know offhand what the SQL for this would look like.

Jon
Jun 27 '08 #2

P: n/a
On Jun 27, 6:47*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 27, 1:15*am, shapper <mdmo...@gmail.comwrote:
Hello,
I have 3 tables with a many to many relationship:
Professors ProfessorID, Age, ...
Tags TagID, Name, ...
ProfessorsTags ProfessorID, TagID
How can I get all the professors and all tags associated to each
professor using LINQ?
I have the following:
* * * List<ProfessorPaperpapers = (from p in database.Professors
* * * * * * * * * * * * * * * * * ** * * orderby p.Name
* * * * * * * * * * * * * * * * * ** * * select new ProfessorPaper {
* * * * * * * * * * * * * * * * * ** * * * Professor = p,
* * * * * * * * * * * * * * * * * ** * * * Tags = new List<Tag{
* * * * * * * * * * * * * * * * * ** * * * }
* * * * * * * * * * * * * * * * * ** * * }
* * * * * * * * * * * * * * * * * ** * * }).ToList();
ProfessorPaper is a wrapper that contains two properties: Professor
and List<Tag>.
In my query what I am missing is filling the tags for each professor.
Could someone, please, tell me how to do this?

Well, you could try:

Tags = from pt in database.ProfessorsTags where pt.Professor == p
select pt.Tag

I don't know how nicely that will translate into SQL though.

Alternatives will depend on how you've mapped the ProfessorsTags
collection. For instance, if you've got two many-to-one relationships
mapped, and you've made both of them bidirectional, you may be able to
do:

Tags = p.ProfessorsTags.Tags

Again, I don't know offhand what the SQL for this would look like.

Jon
Thanks Jon. I will try it
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.