Solved

L2S How do I select Table1 row if a Table2 column matches "Like" data

Posted on 2011-03-14
11
417 Views
Last Modified: 2013-11-11
Tables: part and system, that are related.
A string column - data - exists in table system. Made of numbers separated by a hyphen.
Eg. 123-7643-88241-875
The user provides a number value. If the number is found in this column data,
I want to select the row in table part.
This is similar to the "LIKE %value% in T-SQL.

After the "select a where _", the object b is not recognized in intellisense.
What is the code to explain criteria like this?
I have Visual Basic 2010 Express

Dim x = From a In dc.parts Join b In dc.systems On a.htsn Equals b.tsn _
Select a where _

Open in new window

0
Comment
Question by:gs99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 35135117
Can you restate the question please? I am not understanding what you want.
0
 

Author Comment

by:gs99
ID: 35139059
Let me explain it this way.

Database MyDB has two tables named Person and Ancestor
Column person_id is found in both tables, so there's a relationship.

Table Ancestor includes a column named Lineage.
It's a string made of numbers separated by hyphens.
Example: 4568-17754-9432-4690

The user selects a person_id I call selected_ancestor.

I want to select rows in table Person if the column Lineage in Table Ancestor includes the value in selected_ancestor.  

Example: If selected_ancestor is 17754, the Person row is selected if the related Ancestor row above exists. The hyphen is needed to identify correctly. If lineage had -217754, do not select.

I tried this using T-SQL with the LIKE keyword and %value%.
But I couldn't get it to work.

I felt I may have better success with Linq to SQL.
I have successfully converted other SQL transactions but they've been only one table.
And simpler Where expressions.

Code:
Dim dc As New MyDBDataContext

Dim x = From p in dc.Persons Join a in dc.Ancestors On a.person_ID Equals p.person_ID _
Where xxxxx

Questions:
1. Am i using the correct format (Join, In, On Equals) to recognize the relationship between the tables?
I get no error msg, and after I entered Join, it seemed easy to enter variables following intellisense.

2. What code is needed to select as I need?
Note: After keying in the "On", a is recognized as an object, so I can enter a.personID.
But after entering Where, a is not recognized.
If a column in a is needed, I was disapponted to see that.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 125 total points
ID: 35142167
Hi gs99;

If I understand the requierments correctly this should do what you need.

' Name space needed to get Like function added
Imports System.Data.Linq.SqlClient


' Created DataContext
Dim dc As New DataClasses1DataContext()
' User selected person
Dim selected_personId As Integer = 1
' Lineage string to match in Ancestors table
Dim lineage As String = "17754"

' Because Person is related to Ancestor, one-to-many, you do
' not need to do a join in linq query just use the relationship
Dim result = From p In dc.Persons
             Where p.person_id = selected_personId
             Select p.Ancestors.Where(Function(a) SqlMethods.Like(a.Lineage, "%" & lineage & "%"))

' Iterate over the results
For Each a As Ancestor In result
    ' The a variable is a Ancestor record and
    ' can be accessed by dot operator
    ' a.XXX
Next

Open in new window


Fernando
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:gs99
ID: 35143680

"Select p.Ancestors" does not work.
dc.Ancestors is like dc.Persons, not p.Ancestors

The concept of p.Ancestors is not valid is it?

Thanks for your help.
 
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 35143771
Hi gs99;

You state, "two tables named Person and Ancestor Column person_id is found in both tables, so there's a relationship.", is this relationship defined in the database? If so which side is the one side and which side is the many in the relationship?

Fernando
0
 

Author Comment

by:gs99
ID: 35143844
There is a one-to-one relationship.
For each person_id in one table there is a person_id in the other.

Please note that the explanation I provided here has different object names than my actual application. In my application, one table has a column tsn and the other table has column h_tsn. Both are the only primary keys, and a foreignkey relationship exists.

In the DBML designer, the line that connects the two tables has a diamond on the "Person" table, and a black arrow on the "Ancestor" table.

 
0
 

Author Comment

by:gs99
ID: 35143888
I found this to work. Please note, this is from my application so names are different from my explanation to you.

Dim SA As String = "-" & LTrim(SelectAncestor)
'need to include the hyphen

Dim dc As New ITISDataContext
 
Dim q = From p In dc.taxonomic_units Join h In dc.hierarchies On p.tsn Equals h.h_tsn
              Where (p.kingdom_id = SelectKingdomID And _
              p.parent_tsn <> 0 And _
              p.rank_id = RequestedRank) And _
              SqlMethods.Like(h.hierarchy_string, "%" & SA & "%")
            Select p

Thanks for mentioning the SQLMethods part. I searched for this in google and found an idea that works.
0
 

Author Closing Comment

by:gs99
ID: 35143895
I appreciate the suggestion, even if it was not the final solution.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 35143934
OK lets not work with the relationship and just do the join. In the select if you just want the Ancestors use the code snippet as is. If you both Ancestors and Persons  make the select like this, Select a, p
 
Dim x = From p In dc.Persons _
        Join a In dc.Ancestors On a.person_id Equals p.person_id _
        Where SqlMethods.Like(a.Lineage, "%17754%") _
        Select a

Open in new window

0
 

Author Comment

by:gs99
ID: 35144135
Please see my prior post.

Thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 35144215
Yes I read your prior post.

Not a problem; glad I was able to help.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with Report Viewer in VS2013 6 33
SQL Server for XML PATH giving wrong results. 6 61
Creating a View from a CTE 15 49
Need Help With GDI+ in VB.Net 8 43
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question