Comparable SQL for LINQ query? - linq

Is there an SQL query that would give me the same result as the following LINQ statement?
DataTable dtAll1 = userData1.AsEnumerable()
.Where(ra => !userData2.AsEnumerable()
.Any(rb => rb.Field<string>("A") == ra.Field<string>("B")))
.CopyToDataTable();

SELECT
*
FROM
userData1 AS ra
WHERE
NOT EXISTS (SELECT * FROM userData2 AS rb WHERE ra.B = rb.A)
(Updated from EXISTS to NOT EXISTS.)

Related

How to write SQL query in to Propel Query?

I have following query in SQL.
SELECT * FROM sf_product LEFT JOIN sf_product_m ON sf_product_m.ref_m_id = sf_product.id LEFT JOIN ref_m ON ref_m.id = sf_product_m.ref_m_id;
I'm new to propel and I have no idea how to do this in propel.

NHibernate Linq query join subquery on two column

A "case" can have many action types and each action is logged in a journal.
I want to show a list of "cases" only with the latest journal entry
This is the T-SQL sub query
SELECT SagId, max(Dato) as maxdate FROM vOpgaveliste o group by SagId
And this is the T-SQL main query
select o.* from
(
SELECT SagId, max(Dato) as maxdate
FROM vOpgaveliste o
group by SagId
)
as nyeste
join vOpgaveliste o on o.SagId = nyeste.SagId and o.Dato = nyeste.maxdate
I can create the subquery in linq
var queryInner = from o in query
where o.SagsbehandlerInit == "chr"
where o.Dato >= DateTime.Today && o.Dato <= DateTime.Today.AddDays(-7)
group o by o.SagId
into g
select new { SagId = g.Key, MaxDate = g.Max(d => d.Dato) };
I then created this query
var outer = from o in query
from s in queryInner
where s.SagId == o.SagId && s.MaxDate == o.Dato
select o;
But NHibernate throws a System.NotSupportedException was unhandled by user code exception
I also tried this syntax https://stackoverflow.com/a/16918106/1147577 but get an syntax error on the join statement
Thanks
NHibernate simply does not support sub queries within from statement block. It only supports it in the Select and Where block.
I guess you have to figure out another way to get your result.
Of course the linq to object or many other linq providers support all kind of crazy query constructs, the linq to Nhibernate implementation simply has tons of limitations.

how to write this oracle query in jpa?

Query:
select *
from easquestionsinfo
where questionname in(select questionname
from easresponseinfo
where isconflict = 'yes')
This query works fine and returns me the records from table 'easquestioninfo' when questionname is equal to the one returned by the inner query which returns set of questionname where isconflict='yes'.
JPA supports JPQL, SQL, and Criteria.
You can execute this SQL directly using createNativeQuery().
For JPQL, it depends on your object model, perhaps something like,
Select q fom QuestionInfo q where q.name in (Select r.name from ResponseInfo q2 where r.isConflict = 'yes')
See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL

Convert oracle to JPA createQuery

SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1 WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
In the above oracle query i have to got the follupnum and folluptype in the combination commnumber and commtype. But is it possible to write this query in JPA?
JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery.
You could probably also reword your query in terms of JPQL.
If you are using EclipseLink, it does support sub-selects in the FROM clause in JPQL,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause
JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery
once click this link
Assuming you have Foreign Keys in place between CUSTOMER_MASTER and FOLLOWUP_GUIDES
i.e. Assuming
CUSTOMER_MASTER.FOLL_UP_NUM is FK to FOLLOWUP_GUIDES.COMMNUMBER
CUSTOMER_MASTER.FOLL_UP_TYPE is FK to FOLLOWUP_GUIDES.COMMTYPE
Then your query
SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1
WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
is equivalent to
SELECT t0.*
FROM CUSTOMER_MASTER t0 JOIN FOLLOWUP_GUIDES t1
WHERE t0.COMPANY_ID = 'C001'
AND t1.STAGE_ID = '5'
This can be done in JPA quite easily and looks very similar to this SQL (only using object names and field names instead of table names and attribute names).
JPA JPQL query will look something like this:
SELECT t0
FROM CustomerMaster t0 JOIN FollowupGuides t1
WHERE t0.companyId = "C001"
AND t1.stageId = "5"
Can also do with JPA subquery:
SELECT t0
FROM CustomerMaster t0
WHERE t0.companyId = "C001"
AND EXISTS (SELECT 1
FROM FollowupGuides t1
WHERE t1.commnumber = t0.follUpNum
AND t1.commtype = t0.FollUpType
AND t1.stageId = "5")
=B)

SQL to LINQ query asp.net

I am currently trying to get some statistics for my website but i cant seem to create the query for my database to get the username that if found most frequent in all the rows.
The sql query should look something like this:
SELECT username FROM Views GROUP BY 'username' ORDER BY COUNT(*) DESC LIMIT 1
How do i make that query in my controller?
var username = db.Views.GroupBy(v => v.username).OrderByDescending(g => g.Count()).First().Key
(from a in Views
group a by a.username into b
let c = b.count()
orderby c descending
select a.username).take(1);
Your query conversion .....
This is how you do that query using LINQ:
var temp = (from a in Views
group a.username by a.username into b
orderby b.Count() descending
select b.Key).Take(1);
You can't do LIMIT 1 (mysql), since LinqToSql only generates TSql from MSSqlServer.

Resources