Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello, I'll start by explaining the way the Article and Keyword table are linked. I have three tables. One named Articles, one named Keywords, and the third named KeywordsInUse. Articles and Keywords both have a one to many relationship with the KeywordsInUse table in order to create a many to many relationship between Articles and Keywords.

What I'm trying to do is, filter all the Articles based on if they contain the selected Keyword in them. So for example the user clicks on the keyword, "Dog" and all the articles with the Keyword "Dog" are listed.

Here's the logic that I think might work:
Step 1. Use LINQ to query all the ArticlesInUse records by the selected Keywords ID. Here's my code for this step:
VB
'Declare variable and use the Keyword Label on the form to populate it.
Dim GetKeyID As Integer = lblKeywordID.Text

'Use LINQ to Entities to query all the KeywordsInUse records that have the
'same keyword ID
Dim FindKeywordConnection = (From k In AI.KeywordsInUseTables
                             Where k.KeywordID = GetKeyID
                             Select k)

This works as it should. After doing this step, I now have access to all the Article IDs that are in each of the KeywordsInUse records.

So for example, since there are 4 different records in the KeywordsInUse table that have the selected KeywordID, I now also have the 4 IDs that are linked to the Article table.

This is where I'm having a hard time. What I want to do is display all the articles by their ID. I'm not exactly sure how to do this, but here's how I think I may be able to accomplish this.

Step 2. Loop through and catch and save all the article IDs into an array.

Step 3. Then use the array of article IDs with LINQ to query all the articles by their ID.

The above steps 2 and 3 seem like they'd work, but I just don't know how to do it. My experience with arrays are limited, and I'm currently looking more into them at the time. Plus, while I've been using LINQ to Entities for a few months now, I can't really think of a way where I'd actually be able to use the "Where" clause with an array of IDs.

So to sum up, my questions are: With the above logic work with what I'm trying to accomplish? And if so, can you give me a good push in the right direction of what I need to know in order to accomplish this? Also, if it doesn't work, what can I do that will help me accomplish this?
Posted

1 solution

You are using LINQ but you don't know how to use arrays ? Always use the list classes, they are more flexible. For all that, I think if you want to do this right, you'll do it in one go, probably with a stored proc. Pass in keywords, probably as XML to use OpenXML and then join your temp table to the KeywordsInUse table to get a list of article ids. You can then join to the articles table to get titles, or content.
 
Share this answer
 
Comments
Mr.McCloud 20-Jul-12 9:06am    
Thanks and I know how to use arrays. I just haven't found the need to use them much, so I haven't looked into it as much and my memory is unclear.

Anyways, thanks for the answer. I appreciate it.
Christian Graus 20-Jul-12 17:54pm    
No worries, like I said, using a list is always better, b/c it's a class and you can just add or remove items more efficiently.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900