Linq.How to complete this query?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    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<ProfessorP aperpapers = (from p in database.Profes sors
    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
  • Jon Skeet [C# MVP]

    #2
    Re: Linq.How to complete this query?

    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<ProfessorP aperpapers = (from p in database.Profes sors
                                              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.Profes sorsTags 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.ProfessorsTag s.Tags

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

    Jon

    Comment

    • shapper

      #3
      Re: Linq.How to complete this query?

      On Jun 27, 6:47 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      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<ProfessorP aperpapers = (from p in database.Profes sors
                                               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.Profes sorsTags 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.ProfessorsTag s.Tags
      >
      Again, I don't know offhand what the SQL for this would look like.
      >
      Jon
      Thanks Jon. I will try it

      Comment

      Working...