Linq/Lambda - Question with RIA Services
What I have:
1). I have a listbox and this listbox represents list of categories
2). this listbox has an other listbox in its data template which is the list of boards in each category
3). I use RIA services to fetch the data from the Database, and query using Linq/Lambda statements
4). the data in the database comes from 2 tables a). category which has all categories and 2). board which has all boards for each category.
5). I have these so far ( in the Domain Service )
public IQueryable<discussion_category> GetDiscussion_category()
{
return this.ObjectContext.discussion_category;
}
public IQueryable<discussion_board> GetDiscussion_boardsByCategory(int CategoryID)
{
return this.ObjectContext.discussion_boards.Where(e => e.CategoryID == CategoryID);
}
public IQueryable<discussion_board> GetDiscussion_board()
{
return this.ObjectContext.discussion_board;
}
6). but i want to be able to the following, ( may be using join? can someone help me with the statements? or any other idea? )
What I want to do:
1). I want to fetch the data such that the xaml binding will be like 1st category then its list of boar开发者_JS百科ds then 2nd category and its list of boards etc. 2). I want the Data to be like
Category 1
Board 1
Board 2
Board 3
Category 2
Board 4
Board 5
Board 6
etc
Question: How can i achieve this using Linq/lambda statements?
Have you tried grouping by category?
Here are some good grouping samples: http://msdn.microsoft.com/en-us/vcsharp/aa336754
You could setup a Dictionary<Discussion_category, List<Discussion_board>>
In your data template then bind the ListBoxes to this dictionary accordingly. In order to generate this Dictionary<>, something along the following:
var q = GetDiscussion_category().Select(c =>
new{
Category = c,
Boards = GetDiscussion_boardsByCategory(c.Id).Select(b => b).ToList()
}).ToDictionary(i => i.Category, i => i.Boards);
Edit: You will need to account for querying delays. The above is a general piece of code, to generate the kind of data structure you indicated.
Edit: Also in order to use Dictionary<>
you'll need to ensure that Discussion_category implements Equals()
and GetHashcode()
精彩评论