Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

LinqToSql SubQuery

LinqToSql SubQuery


I have need to perform a sql on the following tables:


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;

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">
        <asp:HyperLinkField DataNavigateUrlFields="OrderId"
            DataTextField="OrderId" NavigateUrl="OrderAdd.aspx" HeaderText="OrderId" SortExpression="OrderId"  />

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

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

Thanks for your input!



RE: LinqToSql SubQuery

I think


<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


select count(1) from [order table]
will allow you to construct the paging links and get much better performance. you can use the 3.5 ListView and DataPager to assist with this type of functionality.

Jason Meckley

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Red Flag This Post

Please let us know here why the post below is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close