CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Question How to query from multiple datatables?

    Hi guys, I’d greatly appreciate if someone could help me with this or give me some pointers. I am trying to create a stock scanner which allows users to filter stocks based on multiple criteria that they select.

    Like the following example with 2 criteria:

    Criteria 1 Filtered Result: StockA, StockB, StockC, StockD
    Criteria 2 Filtered Result: Stock B, Stock C, StockE, StockF

    SCANNER RESULT: Stock B, StockC (ideally this result would be displayed in a GridView)

    Now this seems like an easy task for a simple SQL JOIN statement, but the problem is that the SQL statements I’ve written for some criteria are already so complex that it’s unthinkable to combine them further by using a SQL JOIN statement.

    I think the only way to accomplish this is to put each criteria-filtered result into an array or datatable, and then try to query to see which stocks exist in all the arrays/datatable (in other words satisfy all criteria), and to display these stocks in a gridview. After doing some research online, it is possible to do query from multiple datatables by either using:

    JOINVIEW(DataRelation) or LINQ

    But which one is the best method (if it works)? I didn’t consider using a loop for this since it would be too inefficient, especially if a user select many criteria.

    I’d greatly appreciate if someone could give me some suggestions or simple syntax I can use to achieve this. Thanks in advance!

  2. #2
    Join Date
    Dec 2007
    Posts
    234

    Re: How to query from multiple datatables?

    I would probably go for LINQ... depends on the complexity... What you'll need to look for is the GroupJoin function in LINQ... I think that's the right one. Had to do something similar to this once. If I remember right, it involved creating a list that contained all of the distinct items, then using that list to filter out from the other where items were contained in the list. With some care, it can be done all at once.

    Also look up INTERSECT ... I can't remember just how it's used but there is a function that you can call on one, feeding it the other and it will return rows that are in common. It may be an array extension method though. But it's at least worth looking up.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured