Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-14
11
Medium Priority
?
429 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 500 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 64

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 64

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 64

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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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