This query takes 45ms on postgresql
select buf."Platform", buf."id", buf."idc"
from dbo."tableA" as buf
inner join dbo."TableB" as c
on c."idc" = buf."idc"::bigint
inner join dbo."TableC" as o
ON o."idc" = c."idc" and o."id" = buf."id"::bigint and o."idc" = buf."idc"::bigint
group by buf."Platform", buf."id", buf."idc"
however, when i write this with linq, it takes almos 1 sec. Why it takes so long ? how can i handle this
var List= (from buf in db.TableA
join c in db.TableB on buf.idc equals c.idc.ToString()
join o in db.TableC on new { x = c.idc, y = buf.idc, z = buf.id } equals new { x = o.idc, y = o.idc.ToString(), z = o.id.ToString() }
group new { buf } by new { buf.Platform, buf.id, buf.idc} into grp
select new
{
id= grp.Key.id,
idc= grp.Key.idc,
platform = grp.Key.Platform,
count = grp.Count(),
}
).ToList();
Related
I want to get data like this but it gives me an error on where clause how
do write this query in Linq
var q = (from a in dt.AsEnumerable()
join b in dt2.AsEnumerable() on a.Field<string>("id") equals b.Field<string>("id")
into y
from z in y.DefaultIfEmpty()
where a.Field<int>("ref_id") != b.Field<int>("ref_id")
select new wrongreferralDTO(){
c_id = a.Field<int>("captain_id"),
event_time = a.Field<DateTime>("event_time")
}).ToList();
you should try like this ,
var refIdsToremove = dt2.AsEnumerable().Select(b => b.Field<int>("ref_id")).ToList();
var q = (from a in dt.AsEnumerable()
join b in dt2.AsEnumerable() on a.Field<string>("id") equals b.Field<string>("id")
into y
from z in y.DefaultIfEmpty()
where !refIdsToremove.Contains( a.Field<int>("ref_id") )
select new
{
c_id = z.Field<int>("captain_id"),
event_time = z.Field<DateTime>("event_time")
}).ToList();
as you cannot get access to b after putting join in other variable y, when you do into y b variable go out of scope
I'm trying to join two group by queries to get one results set.
var query = from PP in _db.paymentPlans
join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
join C in _db.Courses on APP.courseID equals C.courseID
where PP.active == true && APP.agentID == agentID
orderby C.courseID ascending
group new {C,PP} by new {C.courseID} into totalRecievable
// Query 1
from PD in _db.paymentDetails
join PP in _db.paymentPlans on PD.paymentPlanID equals PP.paymentPlanID
join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
join C in _db.Courses on APP.courseID equals C.courseID
where PP.active == true && APP.agentID == agentID
orderby C.courseID ascending
group new { C,PD } by new { C.courseID, C.cricosCode, C.courseName } into paymentsCourseWise
// Query 2
select new PdPpAppCourseModel
{
courseID = paymentsCourseWise.Key.courseID,
cricosCode = paymentsCourseWise.Key.cricosCode,
courseName = paymentsCourseWise.Key.courseName,
totalAmount = totalRecievable.Sum(x => x.PP.totalAmount),
paidAmount = paymentsCourseWise.Sum(x => x.PD.paidAmount)
}).ToList();
Total about is taken from query 1 as it should group in payment plan(PP) level.
You can only combine enumerations of the same type, you could project both to a common class and then concatenate them:
var result1 = db1.table.Where(a=>a.value>0).Select( x=> new Foo() { //set props });
var result2 = db2.table.Where(a=>a.value>0).Select( x=> new Foo() { //set props });
var resultSum = result1.Concat(result2);
Similarly you can apply this in your code and join this two groups.
I have a list with a column value like "0000000385242160714132019116002239344.ACK" i need to take last 6 digits from this value like "239344" without extension(.ack) when binding to the list.
And i need to find the sum of Salary field also.
My query looks like below.
var result = from p in Context.A
join e in B on p.Id equals e.Id
join j in Context.C on e.CId equals j.CId
where (e.Date >= periodFrom && e.Date <= periodTo)
group new
{
e,
j
} by new
{
j.J_Id,
e.Date,
e.Es_Id,
e.FileName,
j.Name,
e.ACK_FileName,
p.EmpSalaryId,
p.Salary
} into g
orderby g.Key.CId, g.Key.Es_Id, g.Key.Date, g.Key.FileName
select new
{
CorporateId = g.Key.CId,
ProcessedDate = g.Key.Date,
EstID = g.Key.Es_Id,
FileName = g.Key.FileName,
Name = g.Key.Name,
ack = g.Key.ACK_FileName,
EmpSalaryId = g.Key.EmpSalaryId,
Salary=g.Key.Salary
};
var Abc=result.ToList();
var result = (from p in Context.A
join e in B on p.Id equals e.Id
join j in Context.C on e.CId equals j.CId
where (e.Date >= periodFrom && e.Date <= periodTo)
group new { e, j } by new
{
j.J_Id,
e.Date,
e.Es_Id,
e.FileName,
j.Name,
ACK_FileName = e.ACK_FileName.Substring(e.ACK_FileName.IndexOf(".ACK") - 7, 11),
p.EmpSalaryId,
p.Salary
} into g
orderby g.Key.CId, g.Key.Es_Id, g.Key.Date, g.Key.FileName
select new
{
CorporateId = g.Key.CId,
ProcessedDate = g.Key.Date,
EstID = g.Key.Es_Id,
FileName = g.Key.FileName,
Name = g.Key.Name,
ack = g.Key.ACK_FileName,
EmpSalaryId = g.Key.EmpSalaryId,
Salary = g.Sum(item => item.Salary)
}).ToList();
Can someone explain me why this does not work.
private void AdditionalLoad(List<PersonTypePerson> ptps)
{
using(var _context = new SPIS_Entities())
{
var m = (from ptp in ptps.Where(xx => xx.PersonID == _Person.ID)//_context.PersonTypePerson //
join pt in _context.PersonType on ptp.PersonTypeID equals pt.ID
join pta in _context.PersonTypeAttriubute.Where(p => p.active) on pt.ID equals pta.PersontypeID
select new { persontypeatribute = pta, availableatribute = pta.Attribute }).ToList();
var x = from ii in _context.InformationItem.Where(p=>p.PersonID ==_Person.ID)
join pta in _context.PersonTypeAttriubute.Where(p => p.active) on ii.PersonTypeAtributeID equals pta.ID
select new { persontype = pta.PersonType, attribute = pta.Attribute, information = ii };
var z = (from all in m
join fill in x on all.persontypeatribute.AttributeID equals fill.attribute.ID into ps
from fill in ps.DefaultIfEmpty()
select new
{
persontype = all.persontypeatribute.PersonType, //fill.persontype,
available = all.availableatribute,
attribute = fill.attribute,
information = fill.information
}).ToList();
}
}
The funniest thing is that I get a query result from "m" and "x". When it comes to the point to make the left join ("z") it breaks and throws a nullException, object reference not set to an instance of an object.
I'm unable to convert this SQL query into a working linq statement
select sum(cena), id_auta, max(servis)
from dt_poruchy left outer join mt_auta on dt_poruchy.id_auta=mt_auta.id
where dt_poruchy.servis>=3 group by id_auta;
I tryed something like this but i cant handle the select statement
var auta = from a in MtAuta.FindAll()
join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
from ap2 in ap.DefaultIfEmpty()
where ap2.SERVIS >= 3
group ap2 by ap2.ID into grouped
select new {
I'll appreciate any help!
Based on the limited information provided (which tables are certain fields from?), here is what I came up with.
var auta = from a in MtAuta.FindAll()
let p = a.DtPoruchys.Where(s => s.SERVIS >= 3)
select new
{
Id = a.Id,
CenaSum = p.Sum(c => c.Cena),
Servis = p.Max(s => s.SERVIS)
};
I've reached this solution (supposing "cena" belongs to MtAuta.FindAll()):
var auta = from e in
(from a in DtPoruchy.FindAll()
where a.SERVIS >= 3
join p in MtAuta.FindAll() on a.MtAuta equals p.Id into ap
from ap2 in ap.DefaultIfEmpty()
select new
{
Cena = ap.cena,
IdAuta = a.MtAuta,
Servis = a.servis
})
group e by e.IdAuta into g
select new
{
Cena = g.Sum(e => e.cena),
IdAuta = g.Key,
Servis = g.Max(e => e.servis)
};
I am not sure which table cena and servis are coming from but to create grouped sum you do something like.
select new { Sum = grouped.Sum( x => x.cena ) }
and to get max
select new { Max = grouped.Group.Max( x => x.servis ) }
Here is a good reference for you.
MSDN - 101 LINQ Samples
I've modified your solution little bit and i got it working like this:
var auta = from jo in
(
from a in MtAuta.FindAll()
join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
from ap2 in ap.DefaultIfEmpty()
where ap2.SERVIS >= 3
select new
{
Cena = ap2.CENA,
Idauto = ap2.ID_AUTA,
Servis = ap2.SERVIS
}
)
group jo by jo.Idauto into g
select new
{
Cena = g.Sum(jo => jo.Cena),
IdAuto = g.Key,
Servis = g.Max(jo => jo.Servis)
};
I just curious if this is the best solution?