Need vb.net conversion of the below:
orginal question at LINQ To SQL "Group By"
from S in SERVER
join H in SERVERHDD on S.Server_ID equals H.Server_ID
join FILTER in
(from s in SERVERHDD group s
by new {s.Server_ID, s.Letter}
into groupedServerHDD select new
{
SERVERHDD_ID = groupedServer.Sum(gS=>gS.ServerHDD_ID)
}
)
on H.ServerHDD_ID equals FILTER.SERVERHDD_ID
orderby S.Hostname, H.Letter
select S
Thanks in Advance.
From http://www.developerfusion.com/tools/convert/csharp-to-vb/ and untested.
Dim x = From S In SERVER _
Join H In SERVERHDD On S.Server_ID = H.Server_ID _
Join FILTER In (From s In SERVERHDD _
Group s By New ()IntogroupedServerHDD _
Select New ()) On H.ServerHDD_ID = FILTER.SERVERHDD_ID _
Order By S.Hostname, H.Letter _
Select S
Related
I Tried to run the below Query but its showing an error incorrect syntax near 'unionselect' Can anyone help?
strsql = "select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from eqdet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where PERIL=1" & _
"union" & _
"select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from hudet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=2" & _
"union" & _
"select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from todet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=3" & _
"union" & _
"select a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from fldet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=4" & _
"union" & _
"select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from frdet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=5" & _
"union" & _
"select distinct a.LOCID,locnum,locname,streetname,city,county,statecode,state,peril,SITEDEDAMT,SITELIMAMT,COMBINEDDEDAMT,COMBINEDLIMAMT from trdet a inner join loccvg b on a.LOCID=b.locid inner join loc c on b.LOCID=c.LOCID where peril=6"
Looking at the error message I suspect you are concatinating a string to make the query. unionselect should be two words, seperated by a space. (union select) I think that in your code spaces are missing to separate keywords.
See:
"...where PERIL=1" & _ "union" & _ "select distinct..."
will produce
...where PERIL=1unionselect distinct...
output. Correct code (notice the spaces around the union):
"...where PERIL=1" & _ " union " & _ "select distinct ..."
So i need to make a strsql like this one :
strSQL = "SELECT "
strSQL = strSQL & " tblclips.fldclipid, "
strSQL = strSQL & " tblPlatenfirma.fldplatenfirmaID, "
strSQL = strSQL & " tblClips.fldUitvoerder, "
strSQL = strSQL & " tblClips.fldTitel, "
strSQL = strSQL & " tblPlatenfirma.fldnaam"
strSQL = strSQL & " FROM "
strSQL = strSQL & " tblClips "
strSQL = strSQL & " INNER JOIN tblPlatenfirma "
strSQL = strSQL & " ON tblClips.fldplatenfirmaid = tblPlatenfirma.fldplatenfirmaID"
strWHERE = ""
But i got a SQL code that I don't understand so with the result I can't make a strSQL, I really need this for a school project and i got a few days left. This is the SQL i don't understand:
SELECT tbluurroosterleerkracht.flduurroosterleerkrachtID AS uurroosterID, tblklas.fldnaam AS Klas, tbllokaal.fldnummer AS Hoofdlokaal, tblvak.fldvak AS Vak, tbllokaal_1.fldnaam AS LesLokaal, tbllokaal_2.fldbeschikbaar AS beschikbaar, tblleerkracht.fldnaam AS NaamLeerkrachtNormaal, tblleerkracht.fldvoornaam AS VoornaamLeerkrachtnormaal, tbldag.flddag AS Dag, tbllesuur.fldvan AS Van, tbllesuur.fldtot AS Tot
FROM (((tbllokaal AS tbllokaal_2 RIGHT JOIN (tblvervangingen LEFT JOIN tblleerkracht AS tblleerkracht_1 ON tblvervangingen.fldleerkrachtID = tblleerkracht_1.fldleerkrachtID) ON tbllokaal_2.fldlokaalID = tblvervangingen.fldlokaalID) LEFT JOIN tbltaak ON tblvervangingen.fldtaakID = tbltaak.fldtaakID) LEFT JOIN tblleerkracht AS tblleerkracht_2 ON tbltaak.fldleerkrachtID = tblleerkracht_2.fldleerkrachtID) RIGHT JOIN (((tbllokaal AS tbllokaal_1 INNER JOIN (((tbldag INNER JOIN (tblklas INNER JOIN tbluurroosterleerkracht ON tblklas.fldklasID = tbluurroosterleerkracht.fldklasID) ON tbldag.flddagID = tbluurroosterleerkracht.flddagID) INNER JOIN tblvak ON tbluurroosterleerkracht.fldvakID = tblvak.fldvakID) INNER JOIN tbllokaal ON tblklas.fldlokaalID = tbllokaal.fldlokaalID) ON tbllokaal_1.fldlokaalID = tbluurroosterleerkracht.fldlokaalID) INNER JOIN tbllesuur ON tbluurroosterleerkracht.fldlesuurID = tbllesuur.fldlesuurID) INNER JOIN tblleerkracht ON tbluurroosterleerkracht.fldleerkrachtID = tblleerkracht.fldleerkrachtID) ON tblvervangingen.flduurroosterleerkrachtID = tbluurroosterleerkracht.flduurroosterleerkrachtID
ORDER BY tblleerkracht.fldnaam, tblleerkracht.fldvoornaam, tbldag.flddag, tbllesuur.fldvan;
It is an unusual SQL query. Are you sure it produces the result you want?
When trying to understand code it is useful to format it so that you can clearly see each part of it. This is particularly true for big SQL queries, and code with lots of nested bracketted bits.
Here is the SQL with my rough formatting applied...
SELECT
tbluurroosterleerkracht.flduurroosterleerkrachtID AS uurroosterID,
tblklas.fldnaam AS Klas,
tbllokaal.fldnummer AS Hoofdlokaal,
tblvak.fldvak AS Vak,
tbllokaal_1.fldnaam AS LesLokaal,
tbllokaal_2.fldbeschikbaar AS beschikbaar,
tblleerkracht.fldnaam AS NaamLeerkrachtNormaal,
tblleerkracht.fldvoornaam AS VoornaamLeerkrachtnormaal,
tbldag.flddag AS Dag,
tbllesuur.fldvan AS Van,
tbllesuur.fldtot AS Tot
FROM
(
(
(tbllokaal AS tbllokaal_2
RIGHT JOIN (tblvervangingen
LEFT JOIN tblleerkracht AS tblleerkracht_1
ON tblvervangingen.fldleerkrachtID = tblleerkracht_1.fldleerkrachtID
)
ON tbllokaal_2.fldlokaalID = tblvervangingen.fldlokaalID
)
LEFT JOIN tbltaak
ON tblvervangingen.fldtaakID = tbltaak.fldtaakID
)
LEFT JOIN tblleerkracht AS tblleerkracht_2
ON tbltaak.fldleerkrachtID = tblleerkracht_2.fldleerkrachtID
)
RIGHT JOIN
(
(
(tbllokaal AS tbllokaal_1
INNER JOIN
(
(
(tbldag
INNER JOIN
(tblklas
INNER JOIN tbluurroosterleerkracht
ON tblklas.fldklasID = tbluurroosterleerkracht.fldklasID
)
ON tbldag.flddagID = tbluurroosterleerkracht.flddagID
)
INNER JOIN tblvak
ON tbluurroosterleerkracht.fldvakID = tblvak.fldvakID
)
INNER JOIN tbllokaal
ON tblklas.fldlokaalID = tbllokaal.fldlokaalID
)
ON tbllokaal_1.fldlokaalID = tbluurroosterleerkracht.fldlokaalID
)
INNER JOIN tbllesuur
ON tbluurroosterleerkracht.fldlesuurID = tbllesuur.fldlesuurID
)
INNER JOIN tblleerkracht
ON tbluurroosterleerkracht.fldleerkrachtID = tblleerkracht.fldleerkrachtID
)
ON tblvervangingen.flduurroosterleerkrachtID = tbluurroosterleerkracht.flduurroosterleerkrachtID
ORDER BY
tblleerkracht.fldnaam,
tblleerkracht.fldvoornaam,
tbldag.flddag,
tbllesuur.fldvan;
Is there a specific part that you do not understand?
I think despite not understanding how the SQL works. Your string building method should still work as a way to store the sql in a program variable.
i have a view in MySQL but for some reason i want it to write in MS Access..i have already imported neccessary tables. when i copy the view definaation from MySQL and write it in Access Query Design it gives me error on "Join operation".What's wrong here ?
View -> qryOccupation
select `a`.`Employee_ID` AS `Employee_ID`,`a`.`Employee_Name` AS `Employee_Name`,`a`.`Flat_No` AS `Flat_No`,`a`.`Area` AS `Area`,`a`.`Building_Name` AS `Building_Name`
from (`tblallotment` `a` join `tblflat` `f`)
where ((`a`.`Flat_No` = `f`.`Flat_No`)
and (`f`.`Status` = 'A')
and (not(`a`.`Employee_ID` in
(select `c`.`Employee_ID`
from (`tblallotment` `a` join `tblcancel_allotment` `c`)
where ((`a`.`Employee_ID` = `c`.`Employee_ID`)
and (`c`.`Date_Cancellation` = 0))))))
select a.Employee_ID AS Employee_ID,
a.Employee_Name AS Employee_Name,
a.Flat_No AS Flat_No,
a.Area AS Area,
a.Building_Name AS Building_Name
from tblallotment a
inner join tblflat f
on a.Flat_No = f.Flat_No
where f.Status = 'A'
and (not (a.Employee_ID in
(select c.Employee_ID
from tblallotment a
inner join tblcancel_allotment c
on a.Employee_ID = c.Employee_ID
where c.Date_Cancellation = 0)))
("SELECT [Department Name], " _
& "FirstName & Chr(32) & LastName AS Name " _
& "FROM Departments LEFT JOIN Employees " _
& "ON Departments.[Department ID] = " _
& "Employees.[Department ID] " _
& "ORDER BY [Department Name];")
I'm new to Linq to SQL and I'm trying to transform this SQL into Linq. Could you please help me out.
SELECT *
FROM [dbo].[tblTest]
INNER JOIN [dbo].[tblStationTest] ON [tblTest].[id] = [tblStationTest].[Test_id]
INNER JOIN [dbo].[tblTestType] ON [tblTest].[TestType_id] = [tblTestType].[id]
LEFT OUTER JOIN [dbo].[tblTestOrder] ON [tblTest].[id] = [tblTestOrder].[Test_id]
AND ([tblTestOrder].[TestOrderList_id] = 1)
WHERE ([Station_id] = 1)
What is causing me problem is this condition AND ([TestOrderList_id] = 1)
This condition can't be in Where clase because it will cancel the effect of Left Join
Thanks kurin
you can try something like
from test in db.tblTest
join stationTest in db.tblStationTest on test.id equals stationTest.Test_id
join testType in db.tblTestType on test.TestType_id equals testType.id
join testOrder in db.tblTestOrder on new{Key1 = test.id, Key2= 1} equals new{Key1 = testOrder.Test_id, Key2 = testOrder.TestOrderList_id} into tempOrders
from t in tempOrders.DefaultIfEmpty()
select new{test.Test_id, testType.something, t.somethingelse}
select m.messageID,m.[addeddate],m.message,count(*) as Comments
from intranet.dbo.Blog_Messages AS m inner join
intranet.dbo.Blog_Comments AS c on c.messageid = m.messageid
group by m.messageID,m.[addeddate],m.message
need help converting this to linq to sql
from m in context.Blog_Messages
select new
{
MessageId = m.MessageID,
AddedDate = m.AddedDate,
Message = m.Message,
Comments = m.Blog_Comments.Count()
}
It isn't a direct conversion, but I think that is what you are after.
from m in db.Blog_Messages
join c in db.Blog_Comments on m.MessageID equals c.MessageID
group m by new {
m.MessageID,
m.AddedDate,
m.Message
} into g
select new {
MessageID = g.Key.MessageID,
AddedDate = g.Key.AddedDate,
g.Key.Message,
Comments = g.Count()
}
Dim Msg = From m In db2.Blog_Messages _
Group Join c In db2.Blog_Comments On m.MessageID Equals c.MessageID Into Comments = Group _
Join u In db2.users On m.userID Equals u.userid _
Select New With {
m.Title, m.Message, m.MessageID,
.CommentCount = Comments.Count(),
.date1 = m.AddedDate.ToShortDateString,
.Time = m.AddedDate.ToShortTimeString(),
.fullName = u.fname & " " & u.lname
}
this is what I came up with..
thanks,
chris