473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Linq.How to complete this query?

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
2 1021
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
7
by: =?Utf-8?B?Q2hha3JhdmFydGh5?= | last post by:
Today, after watching the presentation by Amanda Silver at http://channel9.msdn.com/Showpost.aspx?postid=335058 , from Channel 9, started exploring the LINQ features. Surprisingly, few facts...
7
by: =?Utf-8?B?VG9tIEJvbWJhZGls?= | last post by:
Question: Can an outer non-equijoin be simulated using LINQ? It's quite unfortunate that LINQ doesn't support literal joins based on anything other than equality. I know that this the most common...
1
by: john | last post by:
LINQ works a lot like an Access DB Recordset in the way it functions, one of the things I could do in Access is refer to the Fields thru a Variable as below allowing me to Iterate over the...
3
by: shapper | last post by:
Hello, I created an SQL table using the Adjacency Model described here: http://www.sqllessons.com/categories.html By using this model I am able to hold many categories and subcategories in...
24
by: luowan | last post by:
Hi I am currently building an application on .NetCF 3.5 in C#. The application needs to process xml files. I am thinking about using LINQ to process XML. I wrote a piece of code to modify the...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.