Solved

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

Posted on 2011-03-14
11
409 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
  • 6
  • 5
11 Comments
 
LVL 62

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 62

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
 

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 62

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 62

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 62

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
work allocation; web development; vba; access; 4 46
SQl query 19 14
backups - Strategies 1 13
Delete Trigger in SQL Server2008R2 5 13
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now