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

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

gs99Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
 
Fernando SotoRetiredCommented:
Can you restate the question please? I am not understanding what you want.
0
 
gs99Author Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
gs99Author Commented:

"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
 
Fernando SotoRetiredCommented:
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
 
gs99Author Commented:
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
 
gs99Author Commented:
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
 
gs99Author Commented:
I appreciate the suggestion, even if it was not the final solution.
0
 
Fernando SotoRetiredCommented:
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
 
gs99Author Commented:
Please see my prior post.

Thanks
0
 
Fernando SotoRetiredCommented:
Yes I read your prior post.

Not a problem; glad I was able to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.