Can someone help me convert this SQL Query to EntityFramework LINQ. I don't know how to do Inner Join in EF.
select * from UserActivities INNER JOIN LoginHistories
ON UserActivities.iLoginHistoryId = LoginHistories.iLoginHistoryId
AND iUserId = 15
It is just a rough estimate (not enough context):
var db = GetDbContextSomehow();
var query = from ua in db.UserActivities
join lh in db.LoginHistories on ua.iLoginHistoryId equals lh.iLoginHistoryId
where ua.iUserId == 15
select ua;
Related
SQL:
select *
from INVOICE inv WITH(NOLOCK)
inner join ERP_TRANSACTION erpt WITH(NOLOCK) on inv.Id = erpt.Id
inner join ERP_TRANSACTION_LOG erptl WITH(NOLOCK) on inv.InvoiceId = erptl.FimId
Linq:
from inv in _invoiceService.QueryableAsNoTracking()
join erpt in _erpTransactionService.QueryableAsNoTracking() on inv.Id = erpt.Id
join erpt1 _erpTransactionLogService.QueryableAsNoTracking() on inv.InvoiceId = erptl.FimId
select new Model {}
I'm trying to convert my SQL query to a linq query but I couldn't figure out the linq equivalent of WİTH(NOLOCK).
You effectively need to use a "IsolationLevel.ReadUncommitted". It means that the query doesn't care if stuff is in the process of being written to the rows it's reading from - it'll read that "dirty" data and return it as part of the result set.
using (var txn = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadUncommitted
}
))
{
// Your LINQ to SQL query goes here
}
Link to original post - Link
We are working on a SQL query to improve performance. Query is
SELECT
SUM(p.fDebitAmount),
SUM(p.fCreditAmount)
FROM
tSCCompany c
LEFT JOIN
tGLPostSummary p ON (c.fCompanyID = p.fCompanyID AND p.fAccount = c.fEnterpriseAccount AND p.fPostDate <= '2014-08-19 21:51:56')
INNER JOIN
tSCLedgerAccount l ON (c.fCompanyID = l.fCompanyID AND c.fEnterpriseAccount = l.fAccount)
WHERE
c.fEnterpriseID = '4FD5CB57-C04E-11D2-8C59-00A02492E6F3'
AND c.fCompanyID = 'A1F266BA-FC99-11D2-B221-0008C7B1BE09'
AND c.fEnterpriseAccount = '202'
AND c.fCurrencyID = '1'
Above query is taking 6 sec. to execute
Please reference the attached image for further explanation.
Is there any way to reduce these time to improve performance of query?
We try use SUM function with over but it is giving exception like order by syntax error.
We are using index properly on our database.Actually database exist on client side and it is Handel my client. So all the task related to database schema or structure is done by client. We have no permission to update database design. Please suggest me on query level how can we improve performance.
Please try this:
SELECT
SUM(p.fDebitAmount),
SUM(p.fCreditAmount)
FROM tSCCompany c
INNER JOIN tSCLedgerAccount l ON (c.fCompanyID = l.fCompanyID AND c.fEnterpriseAccount =l.fAccount)
LEFT JOIN
tGLPostSummary p ON (c.fCompanyID = p.fCompanyID AND p.fAccount = c.fEnterpriseAccount )
WHERE
c.fEnterpriseID = '4FD5CB57-C04E-11D2-8C59-00A02492E6F3'
AND c.fCompanyID = 'A1F266BA-FC99-11D2-B221-0008C7B1BE09'
AND c.fEnterpriseAccount = '202'
AND c.fCurrencyID = '1' AND Convert(char,p.fPostDate,109) <= Convert(Char,Convert(datetime,'2014-08-19 21:51:56'),109)
I want to write a join statement in LINQ using the dbgeography's "Intersects" method (I am using EF June 2011 CTP). The problem is if I write something like this:
var joinQuery = from spQ in spatialTableQuery
join mnQ in MainQuery
on spQ.Polygon.Intersects(mnQ.PointGeography) equals 1
I get the following error:
The name 'mnQ' is not in scope on the left side of 'equals'. Consider
swapping the expressions on either side of 'equals'.
In SQL I have written a similar query as below so I know SQL suppports it:
SELECT * FROM Address a
INNER JOIN SPATIALTABLE b
WITH(INDEX(geog_sidx))
ON b.geom.STIntersects(a.PointGeography) = 1
Try something like this:
var joinQuery =
from spQ in spatialTableQuery
from mnQ in MainQuery
where spQ.Polygon.Intersects(mnQ.PointGeography) = 1
I have an issue with Linq to Nhibernate producing queries with outer joins.
For example:
return Session.Linq<ClientContact>().Where(c => c.Client.Id = 13).ToList();
Produces a query similar to:
SELECT...
FROM mw_crafru.clientcontact this_,
mw_crafru.client client1_,
mw_crafru.relationshiptype relationsh4_
WHERE this_.clientid = client1_.clientid(+)
AND this_.relationshiptypeid = relationsh4_.relationshiptypeid
AND client1_.clientid = :p0;:p0 = 13
Notice the outer join this.clientid = client1.clientid(+). Boo!
To resolve the issue I went back to using Session.CreateCriteria.
return Session.CreateCriteria(typeof (ClientContact)).Add(Restrictions.Eq("Client.Id", clientId)).List<ClientContact>();
Produces the following query:
SELECT...
FROM mw_crafru.clientcontact this_,
mw_crafru.client client1_,
mw_crafru.relationshiptype relationsh4_
WHERE this_.clientid = client1_.clientid
AND this_.relationshiptypeid = relationsh4_.relationshiptypeid
AND client1_.clientid = :p0;:p0 = 13
Notice no outer join this.clientid = client1.clientid. Yay!
Anyone know why this is happening?
With SQL Server at least, a many-to-one mapping with not-null="true", or Not.Nullable() using Fluent NH, causes Get operations to use an inner join instead of a left join. Try adding that to your mapping for Client in ClientContact.
I've got a sql statement, but I can't get it working in linq. Can someone show me how I can write the following sql statement as linq?
SELECT * FROM mobileApplication
LEFT JOIN videoMobile ON mobileApplication.id = videoMobile.mobileApplicationId
AND videoMobile.videoId = 257
It's a left join with a where statement on the right table. It works in sql server 2005, but I'd like to write it in linq.
I didn't verify the syntax, but try this...
var mobileApplications = from ma in mobileApplication
join vm in videoMobile on ma.id equals vm.mobileApplicationId into j1
from j2 in j1.DefaultIfEmpty()
where vm.videoId == 257
select ma;
There is a product that will do this for you. I have found it very useful. The product name is Linqer. It is not free, but not expensive, and offers a 30 day trial. I have found very few queries it is not able to convert. It has worked well for me.
http://www.sqltolinq.com/
Its something like:
from ma in mobiledApplication.DefaultIfEmpty()
join vm in videoMobile on new { mobileApplicationId = ma.id, videoId = 257 } equals new { mobileApplicationId = vm.mobileApplicationId, videoId = vm.videoId } into videoMobileApplication
from vma in videoMobileApplication
select vma
The keys being the default if empty and using anonymous objects on the join criteria to incorporate 257 into the join.
I am pretty sure that using a where clause for the 257 will achieve the same result though...
Try some like this:
var query =
from m in mobileApplication
join v in videoMobile
on m.id = v.mobileApplicationId and v.id = 257
select m;
See here:
http://msdn.microsoft.com/en-us/library/bb397676%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/magazine/cc163400.aspx