I would like to know if there is a much cleaner looking code to do this LINQ statements because soon as I transfer to my actual program with these queries I will be querying more variables (other properties of the child_t table).
LINQ Statements from LINQpad:
// Displaying the Child (with their dialects)
var query = (
from c in child_t
join cd in tn_child_dialect_t on c.child_id equals cd.child_id into tcdGroup
from a in tcdGroup.DefaultIfEmpty()
join d in lu_dialect_t on a.dialect_id equals d.dialect_id into dGroup
from b in dGroup.DefaultIfEmpty()
group c by new { c.child_id, c.last_name, b.name } into grp
select new
{
child_id = grp.Key.child_id,
last_name = grp.Key.last_name,
dialects = grp.Key.name,
});
var query2 = (from item in query.ToList()
group item by new { item.child_id } into grp
select new
{
child_id = grp.FirstOrDefault().child_id,
last_name = grp.FirstOrDefault().last_name,
dialects = string.Join(", " , grp.Select(d=>d.dialects)),
}).ToList();
// Displaying the Child (with their vaccinations)
var query3 =
from c in child_t
join cv in tn_child_vaccination_t on c.child_id equals cv.child_id into tcvGroup
from n in tcvGroup.DefaultIfEmpty()
join v in lu_vaccination_t on n.vaccination_id equals v.vaccination_id into d2Group
from m in d2Group.DefaultIfEmpty()
//where (c.child_id == 1)
group c by new { c.child_id, c.last_name, m.name } into grp
select new
{
child_id = grp.Key.child_id,
last_name = grp.Key.last_name,
vaccinations = grp.Key.name,
};
var query4 = (from item in query3.ToList()
group item by new { item.child_id } into grp
select new
{
child_id = grp.FirstOrDefault().child_id,
last_name = grp.FirstOrDefault().last_name,
vaccinations = string.Join(", " , grp.Select(d=>d.vaccinations)),
}).ToList();
/* Fuller Outer Join query2 and query4*/
var childIDs = query2.Select(x => x.child_id).Union(query4.Select(x => x.child_id));
var q = from id in childIDs
join d in query2 on id equals d.child_id into firstGroup from n in firstGroup.DefaultIfEmpty()
join v in query4 on id equals v.child_id into secondGroup from m in secondGroup.DefaultIfEmpty()
//where firstGroup.Any() ^ secondGroup.Any()
select new {
id = n.child_id,
dialects = n.dialects,
vaccinations = m.vaccinations
};
Console.WriteLine(q);
This is how it looks like when run:
Description of other related tables:
- child_t = Table with all the info of children
- tn_child_dialect_t = Transaction table that gets the child_id of 'child_t' and dialect_id of 'lu_dialect_t'
- lu_dialect_t = Table with the dialects that can be chosen
- tn_child_vaccination_t = Transaction table that gets the child_id of 'child_t' and vaccination_id of 'lu_vaccination_t'
- lu_vaccination_t = Table with the vaccinations that can be chosen