How to Convert Sql to Linq with Distinct(id) - sql

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();

Related

How to perform following query in LINQ?

I have a SQL query need to do in LINQ. Can anyone helps in converting?
SELECT *
FROM profile
WHERE ProfileId <> 1221 AND IsActive = 1 AND
ProfileId NOT IN (SELECT ReportingPerson
FROM ReportingPersons
WHERE Employee = 1221)
var reportingPerson = context.ReportingPersons.Where(x =>
x.Employee == 1221)
.Select(c => c.ReportingPerson
).ToList();
var result = context.Profiles
.Where(x =>
x.ProfileId != 1221 &&
x.IsActive &&
!reportingPerson.Contains(x.ProfileId)
.ToList();

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

Convert SQL to LINQ with IN and MAX

How would I convert the SQL statement below into LINQ?
select
kcustnum,
(custsnum + 1),
custartype,
kcustsrch
from
custmast
where
kcustnum = 'cn'
and
custsnum in (
select
max(custsnum)
from
custmast
where
kcustnum = 'cn'
)
As #Dai mention you don't need IN since you use MAX.
What you're looking for:
var res = _context.custmast
.Where(x => x.kcustnum == "cn"
&& x.custsnum == _context.custmast
.Where(y => y.kcustnum == 'cn')
.Max(y => y.custsnum))
.Select(x => new
{
x.kcustnum,
(x.custsnum + 1),
x.custartype,
x.kcustsrch
});
this is my code that does not work:
public ActionResult NameSelect(string customerNumber)
{
var shipto = from n in db.cs_Custmast
.Where(x => x.kcustnum == customerNumber
&& x.custsnum == db.cs_Custmast
.Where(y => y.kcustnum == customerNumber)
.Max(y => y.custsnum))
.Select (n => new CustomersViewModel
{
Account = n.kcustnum,
Suffix = (n.custsnum + 1),
AccountType = n.custartype,
Search = n.kcustsrch
});
return View("_NameSelection", shipto);
}

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.

nHibernate, OR clause with .withSubquery

How can we force nHibernate to generate “OR” clause instead of “AND” clause when using “.withSubquery”
var includeSharedTemplateCategories= EntityFinder.Of<TemplateMappers.FolderEntity>() .Where(e => e.MID == effectiveMemberID) .Where(e => e.CategoryType == "shared_template") .SelectList(e => e.Select(c => c.Id));
var includeNormalCategories = EntityFinder.Of<TemplateMappers.FolderEntity>()
.Where(e => e.MID == MemberID)
.Where(e => e.CategoryType == "template")
.SelectList(e => e.Select(c => c.Id));
var query = EntityFinder.Of<TemplateMappers.TemplateEntity>()
.Where(f => f.TemplateIsActive == 1)
.RestrictionByQuery<TemplateMappers.TemplateEntity, TemplateObject>(options)
.WithSubquery.WhereProperty(e => e.CategoryId).In(includeSharedTemplateCategories)
.WithSubquery.WhereProperty(e => e.CategoryId).In(includeNormalCategories)
.SelectByQuery<TemplateMappers.TemplateEntity, TemplateObject>(options)
.OrderByQuery<TemplateMappers.TemplateEntity, TemplateObject>(options);
“And” is used in condition
this_.fkcategoryid IN (SELECT this_0_.pkcategoryid AS y0_
FROM categories this_0_
WHERE this_0_.mid = xyz AND this_0_.categorytype = 's_template')
And this_.fkcategoryid IN (SELECT this_0_.pkcategoryid AS y0_ FROM categories this_0_
WHERE this_0_.mid = abc AND this_0_.categorytype = 'template');
I’m looking for “OR“ clause between sub-queries.
this_.fkcategoryid IN (SELECT this_0_.pkcategoryid AS y0_ FROM categories this_0_ WHERE this_0_.mid = xyz AND this_0_.categorytype = 's_template')
**OR** this_.fkcategoryid IN (SELECT this_0_.pkcategoryid AS y0_ FROM dbo.tblcategories this_0_ WHERE this_0_.mid = abc AND this_0_.categorytype = 'template');
kris.
var includeSharedAndNormalTemplateCategories = EntityFinder.Of<FolderEntity>()
.Where(e => (e.MID == effectiveMemberID && e.CategoryType == "shared_template") ||
(e.MID == MemberID && e.CategoryType == "template"))
.Select(e => e.Id);
var query = EntityFinder.Of<TemplateEntity>()
.Where(f => f.TemplateIsActive)
.RestrictionByQuery<TemplateEntity, TemplateObject>(options)
.WithSubquery.WhereProperty(e => e.CategoryId).In(includeSharedAndNormalTemplateCategories)
.SelectByQuery<TemplateEntity, TemplateObject>(options)
.OrderByQuery<TemplateEntity, TemplateObject>(options);
SideNote: TemplateIsActive should be/is bool no?