There is a table I want to join with different columns in different tables.
This is how so far I did this
var purchData = (from a in db.AppRequest
join e in db.Employee on a.Req_By equals e.Id
join c in db.Company on e.CompanyId equals c.Id
join d in db.Designation on e.DesignId equals d.Id
join l in db.Master_Locations on a.Req_Location equals l.Id
join dep in db.Department on e.DepId equals dep.Id
join p in db.Purchase on a.Id equals p.Req_Id
join pi in db.PurchasingItems on p.Id equals pi.Purchase_Id
join pd in db.PurchasingDetails on p.Id equals pd.Purchase_Id
join pds in db.PurchasingDetailsSup on pd.Id equals pds.PurchasingDetails_Id
join s in db.M_Supplier on pds.Supp_Id equals s.Id
join payMethod in db.Master_PayMethods on s.Pay_Method equals payMethod.Id
join poNo in db.ApprovedPoNumbers on p.Id equals poNo.Purchase_Id
where a.Id == id && pds.IsApproved == true
In db.ApprovedPoNumbers table has purchase_Id and Supplier_Id
In db.PurchasingDetailsSup table has purchase_Id and Supplier_Id
So I want to know that here join poNo in db.ApprovedPoNumbers on p.Id equals poNo.Purchase_Id line I want to join the db.ApprovedPoNumbers table purchase_Id,Supplier_Id with db.PurchasingDetailsSup table purchase_Id and Supplier_Id
You can achieve that by building two objects with the criterias you intend to match and use the equals operator on them:
on new {poNo.purchase_Id, poNo.Supplied_Id} equals new {pds.purchase_Id, pds.Supplier_Id} into details
Related
When allUserDepart is null other join fire a exception on line of join allDepartment. Because uD its null. How to fix this code?
join d in allDepartment on uD.DepartmentId equals d.DepartmentId into departmentJoin
--Ful Code is
var data = (from us in allUsers
join uR in allUserRoles on us.UserId equals uR.UserId into userRoleJoin
from uR in userRoleJoin.DefaultIfEmpty()
join r in allRoles on uR.RoleId equals r.RoleId
join uD in allUserDepartment on us.UserId equals uD.NewUserId into userDepJoin
from uD in userDepJoin.DefaultIfEmpty()
join d in allDepartment on uD.DepartmentId equals d.DepartmentId into departmentJoin
from d in departmentJoin.DefaultIfEmpty()
I am asking for your help regarding a linq query to SQL.
Here is a part of the diagram of my database: https://imgur.com/xFBUm3q
My problem is in the following relationship: tbl_607_bottle and tbl_607_gaz_reporting, I can have several reportings for a bottle but reporting can only have one bottle.
I do this request but it's not satisfying.
var GazReportingWizardViewModel =
(from gr in db.tbl_607_gaz_reporting
join a in db.tbl_607_actors on gr.FK_ID_actors equals a.id
join bo in db.tbl_607_bottle on gr.FK_ID_bottle equals bo.ID
join loc in db.tbl_607_location on bo.FK_ID_location equals loc.ID
join cc in db.tbl_607_conformity_certificate on bo.FK_ID_conformity_certificate equals cc.ID
join j in db.tbl_607_join_bottle_gaz on bo.ID equals j.FK_ID_bottle
join g in db.tbl_607_gaz on j.FK_ID_gaz equals g.ID
join od in db.tbl_607_order_details on g.FK_ID_order_details equals od.ID
where loc.ID == Process
select new GazReportingWizardViewModel
{
bottle_conti_number = bo.bottle_conti_number,
pressure_value = gr.pressure_value,
reporting_date = gr.reporting_date,
first_name = gr.tbl_607_actors.first_name,
last_name = gr.tbl_607_actors.last_name,
expiration_date = cc.expiration_date,
content = od.content_comments
}).Distinct()
.OrderBy(t => t.reporting_date)
.ToList();
I want the last report on each bottle but it return all reports on each bottle.
Would you have an idea of the solution?
Thank you for your time
Thank you so much!
I persisted in trying to solve the problem in a single query when the solution was so simple.
var GazReportingWizardViewModel = (from gr in db.tbl_607_gaz_reporting
join a in db.tbl_607_actors on gr.FK_ID_actors equals a.id
join bo in db.tbl_607_bottle on gr.FK_ID_bottle equals bo.ID
join loc in db.tbl_607_location on bo.FK_ID_location equals loc.ID
join cc in db.tbl_607_conformity_certificate on bo.FK_ID_conformity_certificate equals cc.ID
join j in db.tbl_607_join_bottle_gaz on bo.ID equals j.FK_ID_bottle
join g in db.tbl_607_gaz on j.FK_ID_gaz equals g.ID
join od in db.tbl_607_order_details on g.FK_ID_order_details equals od.ID
where loc.ID == Process
select new GazReportingWizardViewModel
{
bottle_conti_number = bo.bottle_conti_number,
pressure_value = gr.pressure_value,
reporting_date = gr.reporting_date,
first_name = gr.tbl_607_actors.first_name,
last_name = gr.tbl_607_actors.last_name,
expiration_date = cc.expiration_date,
content = od.content_comments
}).ToList();
var res = from element in GazReportingWizardViewModel
group element by element.bottle_conti_number
into groups
select groups.OrderByDescending(p => p.reporting_date).First();
I have a join query and i want to filter the result of this query by using distinct. I want to get only one of the shoes which has same brand, model, primary color and secondary color. How can i make this ? Here is my join query.
var query = from b in db.BrandTbls.AsQueryable()
join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
join s in db.ShoeTbls on m.ModelID equals s.ModelID
join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID
where s.Quantity > 0
orderby m.ModelName
select new
{
s.ShoeID,
m.ModelName,
m.Price,
b.BrandName,
i.ImagePath
};
I found the solution. This query does approximately what i want to do
var query = from b in db.BrandTbls.AsQueryable()
join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
join s in db.ShoeTbls on m.ModelID equals s.ModelID
join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID
group new {b,m,s,i} by new {b.BrandName,m.ModelName,m.Price,s.ShoeID,s.PrimaryColor,s.SecondaryColor,i.ImagePath} into g
select new {g.Key.ShoeID,g.Key.BrandName,g.Key.ModelName,g.Key.ImagePath,g.Key.Price};
Remove price from the output and use Distinct() like this:
var query = (from b in db.BrandTbls.AsQueryable()
join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
join s in db.ShoeTbls on m.ModelID equals s.ModelID
join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID
where s.Quantity > 0
orderby m.ModelName
select new
{
s.ShoeID,
m.ModelName,
b.BrandName,
i.ImagePath
}).Distinct();
Can someone please help me convert this SQL to linq or lambda c#
select
count(s.ClassId) [StudentInClass], c.Name [Class], t.Name [teacher]
from
[dbo].[Students] s
inner join
class c on s.ClassId = c.Id
inner join Teacher t
on t.Id = c.TeacherId
group by
s.ClassId, c.Name, t.Name
so far this is what I have, and i am messing it up. I want to achieve the same results as in my sql query
SchoolEntities db = new SchoolEntities();
var StudentsByCourseId = from s in db.Students
join c in db.Classes on s.ClassId equals c.Id
join t in db.Teachers on c.TeacherId equals t.Id
group c by s.ClassId
into g
select g;
in SQL this is what my reults look like, it counts the students in a class by the teacher
StudentCount Class Teacher
1 Geography Teacher1
1 Biology Teacher1
2 Maths Teacher2
You can use an anonymous class to group by multiple properties.
var StudentsByCourseId = from s in db.Students
join c in db.Classes on s.ClassId equals c.Id
join t in db.Teachers on c.TeacherId equals t.Id
group s by new { s.ClassId, Class = c.Name, Teacher = t.Name }
into g
select new
{
StudentInClass = g.Count(),
g.Key.Class,
g.Key.Teacher,
};
I have a Linq expression that (for northwind) gets the total qty ordered per product for customer id 'ALFKI'
from od in db.OrderDetails
where od.Order.CustomerID == "ALFKI"
group od by od.Product.ProductName into g1
select new { ProductName = g1.Key, Total = g1.Sum(od => od.Quantity) }
This is fine but to fully understand Linq, I want to try and formulate the expression in a world where Linq2Sql doesn't nicely build the property bridges through foreign keys.
For instance, in the above expression, I'm accessing od.Order.CustomerID. I want to assume that od.OrderID is as far as I can go.
Looking at the SQL for the expression, I have:
SELECT SUM(CONVERT(Int,[t0].[Quantity])) AS [Total], [t2].[ProductName]
FROM [Order Details] AS [t0]
INNER JOIN [Orders] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]
INNER JOIN [Products] AS [t2] ON [t2].[ProductID] = [t0].[ProductID]
WHERE [t1].[CustomerID] = #p0
GROUP BY [t2].[ProductName]
This is as far as I've managed to get:
from od in db.OrderDetails
join o in db.Orders on od.OrderID equals o.OrderID
join p in db.Products on od.ProductID equals p.ProductID
where o.CustomerID == "ALFKI"
group od by od.ProductID into g1
select new { ProductName = g1.Key, Total = g1.Sum(od => od.Quantity) }
This is almost there but g1.Key is referring to the ProductID. I can't seem to get at the ProductName and the order quantity at the same time.
Thanks
Try creating a new anonymous type for your group by:
from od in db.OrderDetails
join o in db.Orders on od.OrderID equals o.OrderID
join p in db.Products on od.ProductID equals p.ProductID
where o.CustomerID == "ALFKI"
group od by new { p.ProductID, p.ProductName } into g1
select new {
ProductName = g1.Key.ProductName,
Total = g1.Sum(od => od.Quantity) }
As well as Kevin's answer, using a composite key, I also managed to do it another way:
from item in (from od in db.OrderDetails
join o in db.Orders on od.OrderID equals o.OrderID
join p in db.Products on od.ProductID equals p.ProductID
where o.CustomerID == "ALFKI"
select new {ProductName = p.ProductName, Quantity = od.Quantity }
)
group item by item.ProductName into g1
select new {ProductName = g1.Key, Total = g1.Sum(od=>od.Quantity) }
More generally, if you want to splice all the joins together and refer to any field:
from item in (from od in db.OrderDetails
join o in db.Orders on od.OrderID equals o.OrderID
join p in db.Products on od.ProductID equals p.ProductID
where o.CustomerID == "ALFKI"
select new { p, od }
)
group item by item.p.ProductName into g1
select new { ProductName = g1.Key, Total = g1.Sum(all => all.od.Quantity)}