Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LinqToSql SubQuery

Status
Not open for further replies.

tinac99

Programmer
Jan 17, 2002
58
US
Hi,

I have need to perform a sql on the following tables:
Order
------
order_id
order_date
cust_id

Order_Items
---------------
Order_id
item_id

so I have the following linqtosql:
var query = from o in dc.order
select new
{
OrderId = o.order_id,
OrderDate = o.order_date,
CustId = o.cust_id,
items = (from oi in dc.Order_Items
where oi.order_id == o.orderId
select new { oi.item_id}),

}

Gridview1.DataSource = query;
Gridview1.DataBind();

I plan to have this query in gridview, suppressing repeating columns[orderid, orderdate, custid] if there are multiple items for that order. Can anyone tell me how to code the mark-up tags so that the Gridview1 items column can display the items collection? This is what I have so far:

<asp:GridView ID="GridView1" runat="server" UseAccessibleHeader="True"
DataKeyNames="OrderId" SelectedIndex="1"
AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" PageSize="50">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="OrderId"
DataNavigateUrlFormatString="OrderAdd.aspx?update=true&orderid={0}"
DataTextField="OrderId" NavigateUrl="OrderAdd.aspx" HeaderText="OrderId" SortExpression="OrderId" />

<asp:boundfield datafield="OrderDate" readonly="true" headertext="OrderDate"/>

<asp:TemplateField HeaderText="items" SortExpression="items">
<ItemTemplate>
<asp:ListBox ID="ListBox1" runat="server" DataTextField="items" DataValueField="items" DataSource="items"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Thanks for your input!

Best,

TinaC
 
I think
Code:
<asp:ListBox ID="ListBox1" runat="server" DataSource="items"/>
should do it since items is just a collection of strings. however there are 2 subtle problems with you query.
1. there is a select n+1 issue
2. there is no limit to the amount of data returned.

neither of these issues will be apparent when developing or with small amounts of data, but if you get into 1000s of orders the system will grind to a halt and then crash.

select n+1 exists because you have the initial query for orders and then another query for each set of order items. so if you have 10 orders it will execute 11 queries. 100 orders 101 queries, etc. remote IO calls, like querying the database, are the more expensive operations you can perform so you want to guard against excessive database queries.

without a limit of the number of orders you may return 100s or 1000s of records. this query doesn't appear to have any criteria/predicates either so the entire table of orders and order items will be returned. the linqtosql provider has an implementation for Skip(n) and Take(n) which allow you to query a page of records. using Skip/Take in conjunctions with a total count of all orders
Code:
select count(1) from [order table]
will allow you to construct the paging links and get much better performance. you can use the asp.net 3.5 ListView and DataPager to assist with this type of functionality.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top