Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCameron Byfield
151 PointsLink ID in one list to an ID in another
Hi, I am creating a program which contains two lists - one which is _categories and one which is _items.
Both of the lists have category ID's and the items which are associated with the particular category have matching category ID's.
I need to link them together and I'm using MS Visual Basic to do this but can't get it to work. Here is an example of my coding.
Private _categories As List(Of Categories)
Private _items As List(Of Items)
Public Sub New()
// I have set the properties of the list to be category ID and category name _categories = New List(Of Categories) _categories.Add(New Categories(1, "Items on Wheels")) ' items A0001 to A0005
//I have set the properties of the items list to be int ItemID, string Itemcode, string CategoryID, string Itemdescription and bool onorder _items = New List(Of Items) _items.Add(New Items(1, "A0001", 1, "Horse on Wheels", 5, False)) _items.Add(New Items(2, "A0002", 1, "Elephant on Wheels", 5, False)) _items.Add(New Items(3, "A0003", 1, "Dog on Wheels", 5, False))
//As you can see, all of the ItemIDs above match to the categoryID of Items on Wheels. here is the code that I have written to attempt to get them to match up but it is not working
Public Function Getitems(categoryID As Integer) As IList(Of Categories) Dim result = From i In _items Where i.CategoryID = categoryID Select i Return result.ToList
It would be great if I could get some help!
2 Answers
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey Cameron, if I understand you correctly, you're trying to create a new list that contains all items that have a category, right?
Could you do something like:
public List<Items> getItems(int categoryID, List<Items> _items)
{
List<Items> categoryItems = new List<Items>();
foreach (Items item in _items)
{
if (item.CategoryID == categoryID)
{
categoryItems.Add(item);
}
}
return categoryItems;
}
Also, are you pulling these two lists from a database? If so, you can probably have the database join the Category and Item entities together so the record set will be returned to your application with the entities already joined.
Let me know if I misunderstood something or you have any more questions :)!
James Churchill
Treehouse TeacherCameron,
You don't show the code where you call the Getitems
function. Seeing that function call would be helpful.
Also, try setting a breakpoint in the Getitems
function and debug your app. When you hit your breakpoint you can then verify the value of the categoryID
parameter, which is where I'd start with debugging your problem.
Thanks ~James