Solved

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

Posted on 2011-03-14
11
415 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
converting integer data type to time data type in sql 4 46
asp.net figure out error 3 23
How can I use this function? 3 32
How to force output to ascii 2 36
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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