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?