1

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:

Screenshot of the LINQ statements 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
Fritz
  • 89
  • 7

1 Answers1

0

Try something like this:

var query =
    (from child in child_t
     join tmp_child_dialect in tn_child_dialect_t
         on child.child_id equals tmp_child_dialect.child_id into tmp_child_dialect_group
     from child_dialect in tmp_child_dialect_group.DefaultIfEmpty()
     join tmp_dialect in lu_dialect_t
         on child_dialect.dialect_id equals tmp_dialect.dialect_id into tmp_dialect_group
     from dialect in tmp_dialect_group.DefaultIfEmpty()
     join tmp_child_vaccination in tn_child_vaccination_t
         on child.child_id equals tmp_child_vaccination.child_id into tmp_child_vaccination_group
     from child_vaccination in tmp_child_vaccination_group.DefaultIfEmpty()
     join tmp_vaccination in lu_vaccination_t
         on child_vaccination.vaccination_id equals tmp_vaccination.vaccination_id into tmp_vaccination_group
     from vaccination in tmp_vaccination_group.DefaultIfEmpty()
     select new { ChildId = child.child_id, 
                  DialectName = dialect.name, 
                  VaccinationName = vaccination.name }
     )
     .GroupBy(x => x.ChildId, (key, group) => new
         {
             ChildId = key,
             Dialects = group.Select(x => x.DialectName)
                             .Where(x => x != null)
                             .Distinct(),
             Vaccinations = group.Select(x => x.VaccinationName)
                                 .Where(x => x != null)
                                 .Distinct()
         });
PiKos
  • 1,344
  • 1
  • 16
  • 15
  • Thank you so much! I tried it out and it worked. Also, since I am quite new with LINQ, may I ask a short explanation of this line - especially the (key, group) part? .GroupBy(x => x.ChildId, (key, group) => new {...} – Fritz Sep 22 '16 at 15:50