Linq and nested repeaters

Last post 04-23-2008 7:58 AM by docgecko. 3 replies.

Sort Posts:

  • Linq and nested repeaters

    01-21-2008, 11:06 PM
    • Loading...
    • GabrielX
    • Joined on 01-22-2008, 3:36 AM
    • Posts 5

    I'm a total noob to .NET, ASP.NET, LINQ and so on, so please be gentle. :-)

    I'm trying to display a list of people and the tags that are on those people, something like this:

    Gabriel - this, that, these, those
    John - that, those, another
    Sarah - that, another, something

    I'm using Linq to access the data, and can get the list of people going with this code:

    1    var peoplelist = from p in thing.People
    2 select new {
    3 p.FullName
    4 };
    5
    6 PeopleRepeater.DataSource = peoplelist;
    7 PeopleRepeater.DataBind();
     Now, I have a m->m link between people and tags, and use code like this to display the tags on a single person:
    1    var taglist = (from tag in db.Tags
    2 join persontag in db.PeopleTags on tag.id equals persontag.TagID
    3 where persontag.PersonID == person.PersonID
    4 orderby tag.TagName
    5 select tag);
    6
    7 ViewTagRepeater.DataSource = taglist;
    8 ViewTagRepeater.DataBind();
    I can't for the life of me figure out how to get these to play nicely together using nested repeaters. If I move the ViewTagRepeater into the PeopleRepeater.ItemTemplate, and adjust the query to relate to peoplelist somehow, will everything just kind of work together?
    Thanks! 
  • Re: Linq and nested repeaters

    01-22-2008, 2:38 AM
    Answer
    • Loading...
    • stefan.sedich
    • Joined on 08-29-2007, 10:30 AM
    • Perth, Australia
    • Posts 83

    Hello,

    Assuming your object is called Person, your linked tags is called Tags and a tag has a TagName maybe something like this:

    var peoplelist = from p in thing.People select p;

    // Bind this to repeater which looks like so:

     <asp:Repeater ID="rptPeople" runat="server">
        <ItemTemplate>
            <%# Eval("FullName") %>
            <asp:Repeater id="rptTags" runat="server" DataSource="<%# ((Person)Container.DataItem).Tags  %>">
                <ItemTemplate>
                    <%# Eval("TagName") %>
                </ItemTemplate>
            </asp:Repeater>       
            <br /><br />
        </ItemTemplate>
     </asp:Repeater>

    So basically you select a person who has a link to their tags which are bound in the nested repeater.


    You could also do load options so that your tags are not lazy loaded and only use 2 sql queries instead of 1 X nopeople queries, you would do this on you datacontext assuming it is called db something like so:

                System.Data.Linq.DataLoadOptions opts = new System.Data.Linq.DataLoadOptions();
                opts.LoadWith<Person>(item => item.Tags);
                db.LoadOptions = opts;


    Hope that helps.


    Thanks
    Stefan

  • Re: Linq and nested repeaters

    01-22-2008, 11:39 AM
    • Loading...
    • GabrielX
    • Joined on 01-22-2008, 3:36 AM
    • Posts 5

    Thanks Stefan, that helped a lot. In the end I used Mitsu's technique to bring the M->M Tags link into the Person class, then used your technique to hook up the repeater.

    Thanks again,

    --gX

  • Re: Linq and nested repeaters

    04-23-2008, 7:58 AM
    • Loading...
    • docgecko
    • Joined on 02-18-2008, 6:09 AM
    • Posts 1

    Hi GabrielX,

    I am trying to do the same thing now with Nested Repeaters and using Linq, pretty much the same way as you.  Except in my case, Discography (i.e. lists of CDs) is the main table and Tracks is a subtable: a One-to-Many relationship between Discography and Tracks.

    So I was wondering if you could show me your final code or you could explain how you solved this one, as I am struggling to bind data to my inner Repeater.

    Many thanks,

     

    DocGecko
     

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter