I can't write this query on EntityFrameWork. İt's so simple but I don't know yet EF.* I need 4 table left join on EF.
select ProductId,Name,ValueName,Value from ProductTechnicalInfo p
left join ProductTechnicalInfoParameter pp on pp.ProductTechnicalInfoId = p.Id
left join ProductTechnicalInfoValue v on v.ProductTechicalInfoId = p.Id
left join ProductTechnicalInfoValueParameter vp on vp.TechnicalValuesId=v.Id
where ProductId = 22
My EF code :
var infos = infoProductsRep.Join(transaction.ProductTechnicalInfoParameter, p => p.Id, pp => pp.ProductTechnicalInfoId, (ProductTechnicalInfo, ProductTechnicalInfoParameter) => new
{
ProductTechnicalInfo,
ProductTechnicalInfoParameter
}).Join(transaction.ProductTechnicalInfoValue, p => p.ProductTechnicalInfo.Id, v => v.Id, (Info, Value) => new
{
Info,
Value
}).Join(transaction.ProductTechnicalInfoValueParameter, p => p.Value.Id, vp => vp.TechnicalValuesId, (Info, Result) => new
{
Info,
Result
}).Where(x => x.Info.Info.ProductTechnicalInfo.ProductId == product.Id).ToList();
It can be some issues with property names, but your query is easily convertible to LINQ:
var query =
from p in infoProductsRep
join pp in transaction.ProductTechnicalInfoParameter on p.Id equals pp.ProductTechnicalInfoId into gpp
from pp in gpp.DefaultIfEmpty()
join v in transaction.ProductTechnicalInfoValue on p.Id equals v.ProductTechicalInfoId into gv
from v in gv.DefaultIfEmpty()
join vp in transaction.ProductTechnicalInfoValueParameter on p.Id equals vp.TechnicalValuesId into gvp
from vp in gvp.DefaultIfEmpty()
where p.ProductId == 22
select new
{
p.ProductId,
pp.Name,
v.ValueName,
vp.Value
};
var result = query.ToList();
Related
Hi I try to convert the SQL query to LINQ.
SELECT C.ID, SUM(S.AMOUNT*CS.PRICE) AS TOTALCATEGORYSUMMARY
FROM CATEGORY C
INNER JOIN PRODUCT P ON P.CATEGORYID=C.ID
INNER JOIN PSTOCK S ON S.PRODUCTID=P.ID
INNER JOIN PCOST CS ON CS.PRODUCTID=P.ID
GROUP BY C.ID
I try as below:
var qry = from c in categories
join p in products on c.Id equals p.CategoryId
join s in stoks on p.Id equals s.ProductId
join t in costs on p.Id equals t.ProductId
group new {c} by new { c.Name,s.Stock,t.Amount } into ct
select (new { ct.Key.Name, AllCost=ct.Key.Amount * ct.Key.Stock });
How can I do this?
From your SQL query, your LINQ statement should be:
var qry = from c in categories
join p in products on c.Id equals p.CategoryId
join s in stoks on p.Id equals s.ProductId
join t in costs on p.Id equals t.ProductId
group new { c.Id, s.Amount, t.Price } by c.Id into ct
select new { Id = ct.Key.Id, AllCost = ct.Sum(x => x.Amount * x.Price) };
References
Group by single property
Enumerable.Sum method
How can I do this in LINQ?
select MAX(d.DepartureDateRange),MAX(d.ReturnDateRange)
from Tour t join
TourCategory tc on t.ID = tc.TourID
join TourDates td on t.ID = td.TourID
join Dates d on d.ID = td.DatesID
where tc.CategoryID = 3 and t.ID = 12
Database diagram is here ->
For example joins is like this but i cannot get Max of DepartureDateRange & ReturnDateRange
var query2 = from t in db.Tour
join tc in db.TourCategory on t.ID equals tc.TourID
join td in db.TourDates on t.ID equals td.TourID
join d in db.Dates on td.DatesID equals d.ID
where tc.CategoryID == 3
select new IndexTour
{
ID = t.ID,
TourName = t.TourName,
//DepartureDateRange =
//ReturnDateRange =
Description = t.SmallDesc,
Price = t.Price,
CoverPhotoUrl = t.CoverPhotoUrl,
TourProgram = t.TourDesc
};
Thanks in advance.
Here it is (dates are grouped by Tour):
var query2 =
from t in db.Tour
join tc in db.TourCategory on t.ID equals tc.TourID
where tc.CategoryID == 3
// join dates aggregates grouped by tour id
join tdates in
from td in db.TourDates
join d in db.Dates on td.DatesID equals d.ID
group d by td.TourID into grp
select new
{
tourID = grp.Key,
departure = grp.Max(g => g.DepartureDateRange),
rtrn = grp.Max(g => g.ReturnDateRange)
}
on t.ID equals tdates.tourID
select new IndexTour
{
ID = t.ID,
TourName = t.TourName,
DepartureDateRange = tdates.departure,
ReturnDateRange = tdates.rtrn,
Description = t.SmallDesc,
Price = t.Price,
CoverPhotoUrl = t.CoverPhotoUrl,
TourProgram = t.TourDesc
};
I think this is what you want?
var dateRanges = tours
.Join(tourCategories,
t => t.Id,
tc => tc.TourId,
(t, tc) => (t, tc))
.Join(tourDates,
ttc => ttc.t.Id,
td => td.TourId,
(ttc, td) => (ttc, td))
.Join(dates,
ttctd => ttctd.td.DateId,
d => d.Id,
(ttctd, d) =>
new {
TourId = ttctd.ttc.t.Id,
CategoryId = ttctd.ttc.tc.CategoryId,
DepartureDateRange = d.DepartureDateRange,
ReturnDateRange = d.ReturnDateRange
});
var filtered = dateRanges
.Where(r => r.CategoryId == 3 && r.TourId == 12);
var maxDepartureDateRange = filtered.Max(d => d.DepartureDateRange);
var maxReturnDateRange = filtered.Max(d => d.ReturnDateRange);
I am trying to convert below SQL query to LINQ/Lambda in C#
SELECT DISTINCT M.InternalID, P.Code
FROM (
dbo.MeasureValue MV
INNER JOIN dbo.Measure M ON MV.MeasureID = M.ID
INNER JOIN dbo.Provider P ON MV.ProviderID = P.ID
)
WHERE MV.ReportingDate = (
SELECT MAX(ReportingDate)
FROM (
SELECT ReportingDate
FROM dbo.MeasureValue
WHERE MeasureID = MV.MeasureID
) MaxReportingDate
);
I have got so far,
(from MV in MeasureValues
join M in Measures on MV.MeasureID equals M.ID
join P in Providers on MV.ProviderID equals P.ID
Where //???
select new //Distinct??
{ M.InternalID, P.Code} )
Could someone please guide me how to use nested WHERE condition as in SQL query and do MAX of nested SELECT and DISTINCT on whole?
As a whole the LINQ/Lamda should output same result as SQL query.
*I am new to SQL and LINQ
Thanks in advance.
Try this one:
var query =
from mv in MeasureValues
join m in Measures on mv.MeasureID equals m.ID
join p in Providers on mv.ProviderID equals p.ID
where mv.ReportingDate ==
(from mv2 in MeasureValues
where mv2.MeasureID == mv.MeasureID
orderby mv2.ReportingDate descending
select mv2.ReportingDate
).FirstOrDefault()
select new { m.InternalID, p.Code };
var distinct =
from q in query
group q by new { q.InternalID, q.Code} into gr
select new
{
InternalID = gr.First().InternalID,
Code = gr.First().Code
};
var result = distinct.ToList();
Another option to find max ReportingDate:
var query =
from mv in MeasureValues
join m in Measures on mv.MeasureID equals m.ID
join p in Providers on mv.ProviderID equals p.ID
where mv.ReportingDate == MeasureValues.Where(x => x.MeasureID == mv.MeasureID).Select(x => x.ReportingDate).Max()
select new { m.InternalID, p.Code };
I need help producing the required C# code to implement the following SQL query. I am unsure if this is even possible?
select p.Name, r.Id, r.Status, r.Created, r.DepartmentId
from (select r.*, row_number() over (partition by personid order by created desc) as seqnum
from checkin r
where r.departmentid = '7AF20674-AEC1-4D4C-B1EA-88B1D7E8F3DB' and cast(created as date) = '2013-02-11'
) r join
person p
on r.personid = p.id
where seqnum = 1
order by p.name desc
Before i used to only have one registration per day per person, so the following was possible. Now i can have several registrations per person per day, thats why SQL has been updated.
var query = from b in context.Persons
join c in context.CheckIns
on b.Id equals c.PersonId into JoinedPersCheck
from c in JoinedPersCheck.Where(i => i.Date.Equals(date)).DefaultIfEmpty()
where b.DepartmentId.Equals(departmentId)
orderby b.Name
select new Model.PersonCheckIn
{
CheckIn = MapToCheckInModel(c),
Person = MapToModel(b),
};
return query;
Try this:
var query = context.CheckIns
.Where(p => p.DepartmentId == departmentId && p.Created.Date == date)
.GroupBy(r => r.PersonId)
.Select(g => g.OrderByDescending(p => p.Created).First())
.Join(context.Persons, c => c.PersonId, p => p.Id,
(c,p) => new { p.Name, c.Id, c.Status, c.Created, c.DepartmentId})
.OrderByDescending(f => f.Name);
I'm trying to achieve the following SQL query which works the way I need it to into LINQ. I have 2 separate SQL queries which I join and than select the product if the groups price is greater than an individual price. I'm using LINQ to objects and have an Object Factory for the Purchases and Products table.
select DISTINCT(a.Name) from
(select p.Prod_ID, p.Name, SUM(p.Price) as Total
from Tb_AvailableProduct p, Tb_Purchases o
where p.Prod_ID = o.Prod_ID
group by p.Prod_ID, p.Name) a
JOIN
(select p.Prod_ID, p.Price
from Tb_AvailableProduct p, Tb_Purchases o
where p.Prod_ID = o.Prod_ID) b
on a.Prod_ID = b.Prod_ID
where a.Total > b.Price
I have this first query in Linq, but I only want to select a Product Name if that product's group price is greater than the product's individual price.. i.e more than one product has been sold. I'm trying to accomplish this with a sum and not using a count.
from o in this.myObjectFactory.ThePurchases
join p in this.myObjectFactory.TheProducts.Values
on o.ProductID equals p.ProductID
where o.CustomerID == customer.CustomerID
group p by p.ProductID into query1
select new { ProductID = query1.Key, TotalPurchasesThisYear = query1.Sum (p => p.Price)});
Something like this should probably work (it's very similar to your SQL query):
var result =
from a in
(
from p in TheProducts
join o in ThePurchases
on p.ProductID equals o.ProductID
group p by new { p.ProductID, p.Name, p.Price } into g
select new
{
ProductID = g.Key.ProductID,
Name = g.Key.Name,
Total = g.Sum(i => i.Price)
}
)
join b in
(
from p in TheProducts
join o in ThePurchases
on p.ProductID equals o.ProductID
select new
{
ProductID = p.ProductID,
Price = p.Price
}
)
on a.ProductID equals b.ProductID
where a.Total > b.Price
select a.Name;
result = result.Distinct();