I'm trying to join two datatables of same keyfields.
table1
ID Class
---- -----
1 10
2 9
table2
ID Class
---- -----
1 8
2 7
Result
ID Class1 Class2
1 10 8
2 9 7
following is code for joining between user and userclients you can replace your table and get the result of join
Follwing image is for the c# but will give you idea in detail
Dim user = From u In Users Join uc In UserClients On u.Id = uc.UserId New From { _
u.Id, _
u.FirstName, _
u.LastName, _
uc.MobileNo, _
uc.imeiNO, _
uc.Id _
}
if you are beginner you can look this :
SQL to LINQ ( Visual Representation )
try something like this:
Dim test = From t1 in table1_
Join t2 in table2 on t1.ID Equals t2.ID _
Select ID = t1.ID,
Class1 = t1.Class,
Class2 = t2.Class
Related
I have a gridview and originally I was binding data from one table (Table 1) and it was straightforward
gvUsers.dataSource = class.getUsers()
gvUsers.databind()
Function getUsers () As List (Of Users)
Return (From X in Table1 Select x).ToList()
End Function
However, some of my data are pointing to another table and there is no relationship between tables ( no foreign key)
Table 1
UID Name Age Blood Type Test Type
1 Sam 22 2 3
2 Jane 23 2 4
Table 2
ID Domain Code Description
1 Blood Type A A
2 Blood Type B B
3 Test Type 1 1
4 Test Type 2 2
Currently In the gridView I see Blood Type as values 2 and Test type 3, 4 ( The second table ID) but I should get the Code column values in Table 2.
How can I join those two tables - I know if there is foreign key but the fact a column name is equivelant to row data name makes it hard for me to figure out!
Cheers
Try Code
var result = (from p in Table1.AsEnumerable()
join Blood in Table2.AsEnumerable() on p.BloodType equals Blood .ID
join Test in Table2.AsEnumerable() on p.TestTyoe equals Test .ID
select new With
{
uid = p.UID,
Name = p.name,
Age = p.age,
BloodType = Blood.Code,
TestType = Test .Code
}).ToList();
Sql Query IS :
select UID ,Name,Age,B.Code as BloodType ,T.Code as TestType From Table1
inner join Table2 B on Table1.BloodType = B.ID
inner join Table2 T on Table1.TestType= T.ID
The Used Vb then
Dim result = from p in Table1.AsEnumerable()
join Blood in Table2.AsEnumerable() on p.BloodType equals Blood .ID
join Test in Table2.AsEnumerable() on p.TestTyoe equals Test .ID
select
p.UID,
p.name,
p.age,
Blood.Code,
Test.Code
gvUsers.DataSource = result
Try this one:
select table1.*, table2.* from table1
inner join table2 on table1.Blood Type = table2.id
You can select you desired columns from both tables.
I have two (simplified) tables:
TriggerSensor:
id INTEGER
active INTEGER
TriggerLogic:
programId INTEGER
ts1 INTEGER
ts2 INTEGER
ts3 INTEGER
ts4 INTEGER
The ts1 to ts4 columns contain the id from TriggerSensor table.
So, by adding TriggerLogic rows I create programs with different sensor setups.
Is it possible to make a
SELECT programId
FROM TriggerLogic
WHERE [all triggers are active]?
Something like
SELECT programId
FROM TriggerLogic l
INNER JOIN TriggerSensor s ON l.ts1 = s.id
AND l.ts2 = s.id
AND l.ts3 = s.id
AND l.ts4 = s.id
WHERE s.active = 1
but where the result only returns rows where all four s.active=1?
The SELECT above returns no rows.
You need to join the TriggerSensor table 4 times
SELECT programId
FROM TriggerLogic l
INNER JOIN TriggerSensor s1 ON l.ts1 = s1.id AND s1.active = 1
INNER JOIN TriggerSensor s2 ON l.ts2 = s2.id AND s2.active = 1
INNER JOIN TriggerSensor s3 ON l.ts3 = s3.id AND s3.active = 1
INNER JOIN TriggerSensor s4 ON l.ts4 = s4.id AND s4.active = 1
I am stuck with a query in an Sqlite Database.
The complete table structure is a bit complex so I will make an example that is broken down to my Problem.
I have 2 tables like
T1
_id name v1 v2 v3
1 test 4 3 1
2 to 1 2 2
3 show 2 2 2
4 what 4 2 4
5 I 1 1 1
6 mean 3 3 1
T2
_id name
1 this
2 is
3 a
4 test
v1, v2, v3 are the foreign keys of T2 _id and in combination will result in one string.
In this example:
T1 _id 1: testathis
T1 _id 2: thisisis
T1 _id 3: isisis
T1 _id 4: testistest
T1 _id 5: thisthisthis
T1 _id 6: aatest
And in this resulting string I want to search and get a result table.
For example if the search string is "isi" (in the sql query "%isi%") I want to get a result table like this:
name v1v2v3
to thisisis
show isisis
It is propably quit easy but I am totally stuck here.
I tried already several ways including group_concat() and various combinations of select ... where clauses.
Although I tried a query similar to this one.
But I am allways failing on the fact that the string has to be concated from rows of another table.
Karl, give this is try:
select t1.name, t21.name || t22.name || t23.name as v1v2v3 from t1
join t2 t21 on t1.v1 = t21._id
join t2 t22 on t1.v2 = t22._id
join t2 t23 on t1.v3 = t23._id
where t21.name || t22.name || t23.name like '%isi%'
I don't have SQLite to test it right now, but it should work :)
SELECT
T1.name as name,
tmpV1.name || tmpV2.name || tmpV3.name as v1v2v3
FROM
T1
JOIN
T2 as tmpV1 on T1.v1 = tmpV1._id
JOIN
T2 as tmpV2 on T1.v2 = tmpV2._id
JOIN
T2 as tmpV3 on T1.v3 = tmpV3._id
WHERE
v1v2v3 like '%isi%'
;
Edit: aww Mosty beat me.
I'm having a bit of issue converting the following t-sql statement to Linq (using 4.0 entity framework)
I'm getting
Unable to cast the type
'System.Linq.IOrderedQueryable1' to
type 'System.Linq.IQueryable1'. LINQ
to Entities only supports casting
Entity Data Model primitive types.
T-Sql
SELECT
I.Id
, I.Name
FROM Inventory I
WHERE I.Id in
(
select top 5 applicationId
from LastViewed
group by ApplicationId, SomeUserId
having SomeUserId = #SomeUserId
order by Max(id) desc
)
This is what I have right now (with help from Linqer)
Dim query As IQueryable(Of Inventory) =
From d In ctx.Inventories
Where
((From e In ctx.LastVieweds _
Group e By _
e.ApplicationId, _
e.SomeUserId _
Into g = Group _
Where DfaitEdsId = user _
Order By g.Max(Function(p) p.Id) Descending _
Select New With { _
ApplicationId _
}).Take(5)).Contains(New With {.ApplicationId = d.Id}) _
Select d
it currently crashes when I do this line.
query.ToList()
Thanks for your time.
Your 'problem' is in your very first line. The query you have specified returns an IOrderedQueryable(Of Inventory) that you are trying to assign to a variable of type IQueryable(Of Inventory). You aren't getting the error until query.ToList() thanks to deferred execution. You can let the compiler infer the type if you have 'option infer' set to on in your project and your query would be something like:
Dim query =
From d In ctx.Inventories
Where
(From e In ctx.LastVieweds _
Group e By _
e.ApplicationId, _
e.SomeUserId _
Into g = Group _
Where DfaitEdsId = user _
Order By g.Max(Function(p) p.Id) Descending _
Select ApplicationId Take 5 _
).Contains(d.Id) _
Select d
Or you can just change the type of query to be IOrderedQueryable(Of Inventory)
Note: What you have works fine in Linq to SQL, but Entities is much stricter when it comes to casting.
Edit:
Alright, let's try to dig a little deeper into this. Tell me which line blows up with the following:
Dim innerList = (From e In ctx.LastVieweds _
Group e By _
e.ApplicationId, _
e.SomeUserId _
Into g = Group _
Where DfaitEdsId = user _
Order By g.Max(Function(p) p.Id) Descending _
Select ApplicationId Take 5).ToList()
Dim query = (From d In ctx.Inventories
Where innerList.Contains(d.Id) _
Select d).ToList()
Don't forget to pare the results down a bit if enumerating the queries with 'ToList()' is too much. The types of both variables are now List(Of Inventory)
I've looked all around and spent way to long trying to convert this SQL statement into a Linq statement in VB. I'm sure it would be a good example for others out there - the statement is trying to pull products that have a many-to-many relationship with product categories, and the categories have a hierarchy of parents/children.
Here is the query I am trying to convert:
SELECT P.ProductID, P.ProductName, P.ProductSlug, P.PartNumber
FROM Products AS P
INNER JOIN Products_Categories AS PC ON PC.ProductID = P.ProductID
INNER JOIN Categories AS C ON PC.CategoryID = C.CategoryID
LEFT OUTER JOIN Categories AS P_Cats ON P_Cats.CategoryID = C.Parent
WHERE (C.CategoryID = 9) OR (C.Parent = 9) OR (P_Cats.Parent = 9)
I can get up to the point where I am trying to say "WHERE ... (P_Cats.Parent = 9)" but can't figure that part out.
THANKS!
Figured it out right after posting the question, but here is the answer in case anyone else finds it helpful:
Dim query = (From products In db.Products _
Join PC In db.Products_Categories On PC.ProductID Equals products.ProductID _
Join C In db.Categories On PC.CategoryID Equals C.CategoryID _
Group Join cat In db.Categories On cat.CategoryID Equals C.Parent Into C_Parents = Group _
From cParents In C_Parents.DefaultIfEmpty() _
Where (C.CategoryID = categoryID Or C.Parent = categoryID Or cParents.CategoryID = categoryID) _
And products.IsDeleted = False)
It was the line "From cParents In C_Parents.DefaultIfEmpty()" that I was leaving out.