views:

74

answers:

1

I'm working with ASP.NET 3.5 SP1 and I have a question that I've been stuck on. I have an ASPX page on which I have 2 repeater controls, 1 nested inside the other. From the code behind I have a simple LINQ To Entities query which retrieves the data and I then bind this data to the repeater controls. The ASPX page is shown below:

<asp:Repeater ID="rptrMain" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblName" runat="server"  ></asp:Label>
        <asp:Label ID="lblDescription" runat="server"></asp:Label>                 
        <asp:Repeater Runat="server" ID="rptrSub">
            <ItemTemplate>
                <asp:Label ID="lblPartName" runat="server" ></asp:Label>
                <asp:Label ID="lblManufacturerName" runat="server" ></asp:Label>
            </ItemTemplate>
        </asp:Repeater>
     </ItemTemplate>
</asp:Repeater>

I bind the data to the repeaters in the code behind as follows:

var q = from inventories in itemContext.Inventory.Include("Parts.Manufacturer")
        select inventories;

List<Inventory> inventoryList = q.ToList();
rptrMain.DataSource = inventoryList ;
rptrMain.ItemDataBound += new RepeaterItemEventHandler(rptrMain_ItemDataBound);
rptrMain.DataBind();

void rptrMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Inventory inventory = e.Item.DataItem as Inventory;
        Label lblName = e.Item.FindControl("lblName") as Label;
        Label lblDescription = e.Item.FindControl("lblDescription") as Label;

        lblName.Text = inventory.Name;
        lblDescription.Text = inventory.Description;

        Repeater rptrSub = e.Item.FindControl("rptrSub") as Repeater;
        rptrSub.DataSource = inventory.Parts;
        rptrSub.ItemDataBound += new RepeaterItemEventHandler(rptrSub_ItemDataBound);
        rptrSub.DataBind();
    }
}

void rptrSub_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Part part = e.Item.DataItem as Part;
        Label lblPartName = e.Item.FindControl("lblPartName ") as Label;
        Label lblManufacturerName = e.Item.FindControl("lblManufacturerName") as Label;

        lblPartName.Text = part.Name;
        lblManufacturerName.Text = part.Manufacturer.Name
    }
}

Inventory has a 1 to many relation to Part and Part has a many to one relation with Manufacturer.

Now my question is: How do I filter the child entities of Inventory in the LINQ query? For example I would only like to retrieve all Parts that are of a certain PartType. Like this:

where parts.Type == Tire

I did find this helpful tip http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx Although that still does not allow me to filter the child entities?

A: 

i think the best way is to filter the datasource which is used for rptrMain before calling DataBind(). for example the code maybe something like this:

var q = from inventories in itemContext.Inventory.Include("Parts.Manufacturer")
        where inventories.Part.Type == Tire
        select inventories;
...

edited after 1st comment:

i declared two new classes including needed fields for formatting the query:

class AbstractPartsClass
{
    public string Name { get; set; }
    public string ManufacturerName { get; set; }
}
class AbstractInventoryClass
{
    public string Name { get; set; }
    public string Description { get; set; }
    public List<AbstractPartsClass> AbstractParts { get; set; }
}

then i changed the query and some part of your code as below:

var q = from inventory in itemContext.Inventory.Include("Parts.Manufacturer")
        let filteredParts = from part in inventory.Parts
                            where part.Type == Tire //any filter condition
                            select new AbstractPartsClass()
                            {
                                Name = part.Name,
                                ManufacturerName = part.ManufacturerName
                            }
        select new AbstractInventoryClass()
        {
            Name = inventory.Name,
            Description = inventory.Description,
            AbstractParts = filteredParts.ToList(),
        };
List<AbstractInventoryClass> inventoryList = q.ToList();
rptrMain.DataSource = inventoryList ;
rptrMain.ItemDataBound += new RepeaterItemEventHandler(rptrMain_ItemDataBound);
rptrMain.DataBind();

void rptrMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Inventory inventory = e.Item.DataItem as AbstractInventoryClass; //changed line
        Label lblName = e.Item.FindControl("lblName") as Label;
        Label lblDescription = e.Item.FindControl("lblDescription") as Label;

        lblName.Text = inventory.Name;
        lblDescription.Text = inventory.Description;

        Repeater rptrSub = e.Item.FindControl("rptrSub") as Repeater;
        rptrSub.DataSource = inventory.AbstractParts; //changed line
        rptrSub.ItemDataBound += new RepeaterItemEventHandler(rptrSub_ItemDataBound);
        rptrSub.DataBind();
    }
}
void rptrSub_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Part part = e.Item.DataItem as AbstractPartsClass; //changed line
        Label lblPartName = e.Item.FindControl("lblPartName ") as Label;
        Label lblManufacturerName = e.Item.FindControl("lblManufacturerName") as Label;

        lblPartName.Text = part.Name;
        lblManufacturerName.Text = part.ManufacturerName
    }
}
syntaxcheck
Inventory has a 1 to many relationship with Parts so I can't access Parts like that only vice versa "parts.Inventory.Name == "Something"" I will add it to my question to clarify.
Freek
please look at my edited post. ive changed and add something new.
syntaxcheck