Linq - How to Use NOT IN - sql

I am having following query & i need to write it in linq. I am stuck in NOT IN part.
SELECT A.CODE,
A.DATETIME,
A.DATE
FROM TABLE_IO A
WHERE A.DATE>= '01/06/2015' AND A.DATE<='01/06/2015'
AND A.CODE NOT IN(
SELECT CODE
FROM TABLE_ENTRY B
WHERE A.CODE=B.CODE AND A.DATE=B.ENTRY_DATE AND METHOD='M'
)
How to write NOT IN part?
var data = ctx.TABLE_IO.Where(m=>m.Date >= '01/06/2015' && m.Date <= '01/06/2015')
.Select(m=>m).ToList();

You can use !Any:
DateTime dateToCompare = new DateTime(2015, 6, 1);
var data = ctx.TABLE_IO
.Where(m => m.Date >= dateToCompare && m.Date <= dateToCompare)
.Where(m => !ctx.TABLE_ENTRY
.Any(te => m.Code == te.Code && m.Date == te.ENTRY_DATE && te.METHOD == "M"))
.ToList();
I would prefer this, i'm fairly sure that it will be translated to a performant NOT EXISTS which has also no issues with null values like NOT IN/Contains.
The direct translation of NOT IN/Contains would be this:
var data = ctx.TABLE_IO
.Where(m => m.Date >= dateToCompare && m.Date <= dateToCompare)
.Where(m => !ctx.TABLE_ENTRY.Select(te => te.Code).Contains(m.Code))
.ToList();

Basically you can do something such as (pseudo queries below)
var exclusions = table_B.Where(b => [exclusion condition] ).Select(b => b.Id)
var data = ctx.TABLE_IO.Where(m => !exclusions.Contains(m.Id))

Related

How to Convert Sql to Linq with Distinct(id)

select count(distinct LicencePlate) from MT_Vehicle where IsDeleted=0 and CreatedBy = 1
var count = MT_Vehicle.Where(x => x.IsDeleted==0 && x.CreatedBy == 1)
.Select(x => x.LicencePlate)
.Distinct()
.Count();
You can write that as:
var count = db.MT_Vehicle
.Where( v => v.IsDeleted == 0 && v.CreatedBy == 1 )
.Select(v => v.LicencePlate)
.Distinct()
.Count();

SQL with max(date) to LINQ

I have tried to make the following sql in linq with no luck can some help mw with this?
select * from customer c
where companynumber = 1
and status <> 0
and lastdate = (select max(lastdate) from customer where customernumber = c.customernumber)
Gives 22 records
My best try was this:
_ctx.Customers
.Where(r => r.CompanyNumber == companyNumber && r.CustomerNumber != null && r.Status != 0)
.GroupBy(c => c.CustomerNumber)
.Select(g => new
{
name = g.Key,
count = g.Count(),
date = g.Max(x => x.LastEdited)
})
.OrderBy(c => c.name);
Gives 22.000+ records
But not the result as the above SQL
UPDATE:
The following LINQ does the trick ;o)
from a in _ctx.Customers
where a.CustomerNumber != null && a.CompanyNumber == companyNumber
group a by new { a.CustomerNumber } into g
select g.OrderByDescending(a => a.LastEdited).FirstOrDefault() into c
where c.Status == 1
select c
I had to move the where c.Status == 1 into my first select statment

How to convert the following SQL query to Linq?

How to convert following SQL query to Linq?
select day(Ser_CallDate) as OrderDate, count(*) TotalCall
from ServiceCalls
where month(Ser_CallDate) = MONTH(getdate()) and year(Ser_CallDate) = YEAR(getdate())
group by day(Ser_CallDate)
Please help me, thanks
var result = ServiceCalls
.Where(s => s.Ser_CallDate.Month == DateTime.Now.Month
&& s.Ser_CallDate.Year == DateTime.Now.Year)
.GroupBy(s => s.Ser_CallDate)
.Select(g => new {
OrderDate = g.Key,
TotalCall = g.Count()})
.ToList();

How can I convert a SQL script into Linq

I do have a sql script that I can't translate into linq. May one of you can help me out to get the right direction.
My big problems are the count and the group by:
SELECT
wfs.ServerId,
COUNT(wfss.Name) AS Records,
wfs.DiskId
FROM WorkflowStep wfs
INNER JOIN WorkflowStepStatus wfss ON wfs.WorkflowStepStatusId = wfss.Id
WHERE wfs.WorkflowId = (
SELECT
Id
FROM Workflow wf
WHERE wf.Name = 'Collecting data virutal'
)
AND wfs.StepNumber IN (1, 2, 3)
AND wfss.Name = 'Processed'
GROUP BY wfs.ServerId,
wfss.Name,
wfs.DiskId
this should work
var result = wfs.Join(wfss,
t => t.WorkflowStepStatusId,
u => u.ID,
(t, u) => new {
t.ServerID,
t.WorkflowId,
t.StepNumber,
u.Name,
t.DiskID
})
.Where(t => t.WorkflowId == wf.FirstOrDefault(u => u.Name == "Collecting data virutal").ID &&
t.Name == "Processed" &&
new List<int> { 1, 2, 3 }.ToArray().Contains(t.StepNumber))
.GroupBy(t => new { t.ServerID, t.Name, t.DiskID })
.Select(t => new {
t.Key.ServerID,
Records = t.Key.Name.Count(),
t.Key.DiskID
})
.ToList();

Nhibernate QueryOver: Count in where clause

Any tips on how to convert the following to QueryOver:
var widget = session.Query<Widget>()
.Fetch(x => x.NotificationJobs)
.Where(x =>
x.Status == Status.Active &&
!x.NotificationJobs.Any())
.OrderByDescending(x => x.DateCreated)
.Take(1)
.SingleOrDefault();
Want to get a widget that has no notification jobs.
var widgetWithNoNotificationJob = session.QueryOver<Widget>()
.Where( x => x.Status == Status.Active )
.OrderBy( x => x.DateCreated ).Desc
.Left.JoinQueryOver<NotificationJob>( x => x.NotificationJobs )
.Where( x => x.NotificationJobId == null )
.Take( 1 )
.SingleOrDefault();
This will produce SQL with a LEFT OUTER JOIN on the NotificationJob table and a WHERE clause with NotificationJob.NotificationJobId IS NULL.
Hopefully this will point you in the right direction.