I have two tables:

I want to fill an ASP.NET server control drop down list with all of the authors that have books assigned to them and additionally append the number of items for each author to the list.
I have successfully done so. However it is slow with just this one and I plan to add 3 or 4 more drop down lists doing the same thing. Is there a better way to reform the following code?
using (myDataContext db = new myDataContext())
{
//ddlAuthors
var authors = db.Authors.OrderBy(x=> x.text).Select(x => new
{
authorText= x.text,
authorId = x.authorID,
authorCnt = x.ItemAuthors.Count()
});
ddlAuthor.DataSource = authors;
ddlAuthor.DataTextField = "authorText";
ddlAuthor.DataValueField = "authorId";
ddlAuthor.DataBind();
foreach (ListItem item in ddlAuthor.Items)
{
foreach (var n in authors)
{
if(item.Value == n.authorId.ToString())
item.Text += string.Format(" ({0})", n.authorCnt);
}
}
}
This code might be part of the problem.
If ddlAuthor has 10 items, you’re going to go through this loop 100 times (1 time for each item in ddlAuthor.Items in the outer loop, and then 1 time for each author in the inner loop).
How about if in your query you did:
Then it’s bound in the query, you don’t have to loop through each item and append the count.
I just tried out something similar in LinqPad and it formatted the results like I wanted.