Linq left outer join and group by issue - linq

Hi All my following query returns results like this:
lstView.DataSource = (from h in context.HolidayMains
join hd in context.HolidayDetails on h.Id equals hd.HolidayMainId into hd2
from hd in hd2.DefaultIfEmpty()
join e in context.Employees on h.CreatedBy equals e.Id into e2
from e in e2.DefaultIfEmpty()
join o in context.OfficeLocations on hd.OfficeLocation equals o.Id into o2
from o in o2.DefaultIfEmpty()
select new
{
h.HolidayTitle,
h.Date,
OfficeLocation = o.Name,
CreatedBy = e.Name,
h.Id
}).ToList();
But I need result like this :
How it can be done?

Related

cast to value type 'System.Int32' failed because the materialized value is null

I have a Linq Query but it gives above error I guess due to NULL as I have Module,Block and semester as nullable int when semester column is having value than Module and Block would be null and when Module and block would be null than semster is having value How to handle the below query.
var test1 = (from c in db.StudentCoursesAssigned
join e in db.Years
on c.Year_Id equals e.Id into table1 from e in table1.DefaultIfEmpty()
join cc in db.Courses
on c.Course_Id equals cc.Course_Id into table2 from cc in table2.DefaultIfEmpty()
join g in db.grades
on c.Grade equals g.Id into table3 from g in table3.DefaultIfEmpty()
join m in db.Moduels
on c.Module_Id equals m.Id into table4 from m in table4.DefaultIfEmpty()
join p in db.Programs
on c.Program_Id equals p.Id into table5 from p in table5.DefaultIfEmpty()
join b in db.Blocks
on c.Block_Id equals b.Id into table6 from b in table6.DefaultIfEmpty()
join s in db.Semesters
on c.Semster_Id equals s.Semester_Id into table7 from s in table7.DefaultIfEmpty()
join ss in db.Students
on c.Student_id equals ss.Student_Id into table8 from ss in table8.DefaultIfEmpty()
select new
{
Student_Name=ss.Student_FName,
Course=cc.Course_Name,
Active=c.Active,
Course_start_date=c.Enrolment_Start,
Course_End_date=c.Enrolment_End,
Grade=g.Name,
Module=m.Name,
Program=p.Program_Title,
Year=e.Id,
Semester=s.Semester_Title,
blocks=b.Id
}).ToList();
That is the right Linq query which solves
var test1 = (from c in db.StudentCoursesAssigned
join e in db.Years
on c.Year_Id equals e.Id into table1 from e in table1.DefaultIfEmpty()
join cc in db.Courses
on c.Course_Id equals cc.Course_Id into table2 from cc in table2.DefaultIfEmpty()
join g in db.grades
on c.Grade equals g.Id into table3 from g in table3.DefaultIfEmpty()
join m in db.Moduels
on c.Module_Id equals m.Id into table4 from m in table4.DefaultIfEmpty()
join p in db.Programs
on c.Program_Id equals p.Id into table5 from p in table5.DefaultIfEmpty()
join b in db.Blocks
on c.Block_Id equals b.Id into table6 from b in table6.DefaultIfEmpty()
join s in db.Semesters
on c.Semster_Id equals s.Semester_Id into table7 from s in table7.DefaultIfEmpty()
join ss in db.Students
on c.Student_id equals ss.Student_Id into table8 from ss in table8.DefaultIfEmpty()
select new
{
Student_Name = ss.Student_FName == null ? "No Value" : ss.Student_FName,
Course = cc.Course_Name == null ? "No Value" : cc.Course_Name,
Active = c.Active ,
Course_start_date = c.Enrolment_Start,
Course_End_date = c.Enrolment_End ,
Grade = g.Name == null ? "No Value" : g.Name,
Module = m.Name == null ? "No value" : m.Name,
Program=p.Program_Title == null ? "No Value" : p.Program_Title,
Year=e.Name,
Semester=s.Semester_Title == null ? "No Value" : s.Semester_Title,
blocks=b.Name == null ? "No Value":b.Name,
student_Id=ss.Student_Id,
id=c.Id,
}).ToList();
```

Multiple column join in linq

I want to convert this sql query to linq
SELECT Students.StdId, Mark.Value
FROM Departments INNER JOIN
Mark ON Departments.DepId = Mark.DepId INNER JOIN
Students ON Departments.DepId = Students.DepId AND Mark.StdId = Students.StdId
I did this, but it's wont working
from s in Students
join d in Departments on s.DepId equals d.DepId
join m in Marks on new {s.StdId, d.DepId} equals new {m.StdId, m.DepId}
select new{
SId=s.StdId,
Value=m.Value
}
Lets try with
from s in Students
join d in Departments on s.DepId equals d.DepId
join m in Marks
on new {StdId = s.StdId, DepId = d.DepId} equals new {StdId = m.StdId, DepId= m.DepId}
select new{
SId=s.StdId,
Value=m.Value
}
How about
from s in Students
join m in Marks on s.StdId equals m.StdId
join d in Departments on m.DepId equals d.DepId
select new { s.StdId, m.Value };

multiple tables join by Linq

I have 3 tables which named Player, PlayerDetails and Team. I want to write a linq statement such as
from p in Player join d in PlayerDetails on p.ID equals d.PID
and then right join team table, Player has a column named TID which face to Team table's ID.
I have tried to write a statement like
from p in Player join d in PlayerDetails on p.ID equals d.PID into PlayerGroup
from t in team join g in PlayerGroup on t.ID equals g.p.ID
It certainly can't work. I'm not sure how to write such type of query statement, table left join table2 then right join table3.
Who can help?
I believe you could do something like this:
var LeftJoin =
from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
};
var RightJoin =
from t in Team
join l in LeftJoin on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};
To do everything in one query, I think you could do (not tested) something like this:
var RightJoin =
from t in Team
join l in
(from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
})
on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};

How to get an outerjoin on a Linq query

Given this linq query
from c in context.Customers
from o in c.Orders
where c.City == "MyCity" || o.ShipTo == "MyCity"
select c
the query will not return any rows if the customer's city is "MyCity" but does not have any orders. This is because of the implied inner join between Customers and Orders. How do I select customers with a City of "MyCity" or order shipped to "MyCity
In this case I need an outer join between Customers and Orders. How do I express that in Linq? I think the approximate TSQL would be
select customers.*
from customers
left join orders on customers.id = orders.customerid
where customers.city = 'MyCity' or orders.ShipTo = 'MyCity'
To get an outer join, you can use DefaultIfEmpty. See this question: Linq to Sql: Multiple left outer joins
from c in context.Customers
from o in context.Orders
.Where(a => a.customerid == c.id)
.DefaultIfEmpty()
where c.City == "MyCity" || o.ShipTo == "MyCity"
select c
Alternatively, you can do this:
from c in context.Customers
join o in context.Orders on c.id equals o.customerid into g
from x in g.DefaultIfEmpty()
where c.City == "MyCity" || x.ShipTo == "MyCity"
select c
I believe they both generate the same SQL.
You have to use DefaultIfEmpty
I think something like this would work
var result = from c in context.Customers
join o in c.Orders on c.CustomerId equals o.CustomerId into lj
from or in lj.DefaultIfEmpty()
where c.City == "MyCity" || or.ShipTo == "MyCity"
select c

left outer join problem

I need to convert some SQL statement to LINQ. How convert LEFT OUTER JOIN to equivalent LINQ statement?
You need to use the DefaultIfEmpty operator. The below code should result in a left outer join.
var q = from c in customers
join o in orders on c.Key equals o.Key into g
from o in g.DefaultIfEmpty()
select new {Name = c.Name, OrderNumber = o == null ? "(no orders)" : o.OrderNumber};
Credit to: http://www.hookedonlinq.com/OuterJoinSample.ashx

Resources