Convert to LINQ using inner join on group - linq

Does anyone know if there is a tool to convert T-SQL code to LINQ?
I'm struggling to find a way of converting the following SQL code to LINQ:
select * from actions as a
inner join
(
select max(actionid) as maxactionid, threaduid from actions as a
where a.actiontypeid not in (3,4)
group by threaduid
) as a2 on a2.maxactionid = a.actionid
where a.userid <> 2
Any help appreciated!

check out: http://www.sqltolinq.com/

There is linqer from http://www.sqltolinq.com/.
Linqer is a SQL to LINQ converter tool. It helps you to learn LINQ and convert your existing SQL statements.

Related

Need help understanding how to convert sql statement to (Linq) or (Linq To SQL)

Hi I need some help coverting this sql statement to Linq, I am very new to Linq and LinqToSql, and this is my weakness, it seems like this is used frequently and I need to wrap my brain around the syntax. The Code is below.
select distinct t1.Color from [ProductAttributes] t1 join [Product] t2 on t1.Name = t2.ProductName where t1.ProductID = #productID order by t1.color
#productID is the parameter coming into the function, where I am trying to use Linq in MVC.
Thanks
It might be like this I guess
int myProductID = 1;//or whatever id you want.
MyDataContext mdc = new MyDataContext(CONNECTION_STRING_IF_NEEDED);
//MyDataContext is your datacontext generated by LinqToSql
var result = (from x in mdc.ProductAttributes
join y in Products on x.Name.equals(y.ProductName)
where x.ProductID = myProductID
orderby x.color
select x.Color).Distinct();
Note That Table names might need to be fixed.

Linq (left join) and Sql

SELECT
SI_2.[StudGUID] ,SI_2.[ClassName],
SI_2.[StudGUID],SI_1.[StuName],SI_1.[Title],SI_1.[StuMobileNumber]
FROM
[StudentInfo2] AS SI_2
INNER JOIN
[StudentInfo] AS SI_1 ON SI_2.[StudGUID]=SI_1.[StuCode]
WHERE
SI_2.[ClassID] IN (SELECT SC.[ClassID]
FROM [SchoolClass] AS SC
LEFT JOIN [BookClass] AS BC ON SC.[ClassID]=BC.[ClassID]
WHERE BC.[ClassID] IS NULL AND SchoolYear = 2015)
AND SI_2.[isMonitor] = 1
Question: I want use Linq to implement the code in SQL. How to use Linq to implement the code?
Seek help!

How can I convert sql to linq

This is my SQL query
SELECT
sys.sysobjects.name Name,
sys.foreign_keys.*
FROM
sys.foreign_keys
inner join sys.sysobjects on
sys.foreign_keys.parent_object_id = sys.sysobjects.id
WHERE
referenced_object_id = OBJECT_ID(N'[dbo].[Country]')
I have installed Linqer to convert SQL to linq.
But I got an error:
SQL cannot be converted to LINQ: Table [foreign_keys] not found in the current Data Context.
I am a beginner in Linq. Can Anyone help me to convert SQL to Linq
The problem is that system views will not be picked up by Linqer. If you want to read these tables in your application, first create your own views on them, as was done here and write a query on these views.
CREATE VIEW SysObjectsView AS SELECT * FROM sys.sysobjects;
GO
CREATE VIEW SysForeignKeysView AS SELECT * FROM sys.foreign_keys;
GO
SELECT obj.name Name, fk.*
FROM SysForeignKeysView fk
INNER JOIN SysObjectsView obj ON fk.parent_object_id = obj.id
INNER JOIN SysObjectsView objfk ON fk.referenced_object_id = objfk.id
WHERE objfk.name = N'Country'
Linqer should be able to pick up these views.

Convert oracle query to HQL by using subquery

I'm really confusing about sub Query of hibernate.
I've standard oracle query but unable to convert it into HQL.
select distinct b.nameId
from
(
select nameId from seg_user where id=1
)a, seg_user b
where b.id=a.nameId
can somebody convert it to HQL by using SubQuery or Crieteria
select distinct b.nameId
from seg_user b
where b.id = some (
select a.nameId from seg_user a where a.id=1
)
You can see how to use subqueries here: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

HQL query to join 2 tables with the same key

Iam trying to this in HQL:
select A.a A.a1, B.b,B.b1 from A,B
where A.x=B.x;
It is simple to realize the join with sql but when returninig in HQL I find a problem.
would you please give me the HQL syntax for the join
Thanks for help.
Maybe something like this would work:
select ai.a, ai.a1, bi.b, bi.b1
from A ai, B bi where ai.x = bi.x

Resources