Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GroupBy then Select with conditional record addition

I am having LINQ query in which I have to response with a result set which is depending on inner field collection. I have done the thing via LINQ query then a foreach but I wanted to avoid foreach loop and do it somehow from group by

List<ResultModel> result = new List<ResultModel>();           
var tempResultSet = _context.MainRecordTable.Where(h => h.id)
    .Select(lev => new 
    {
        Contacts = (lev.basetable2 != null 
                            && lev.basetable2.basetable3 != null 
                            && lev.basetable2.basetable3.basetable6 != null
                            &&  lev.basetable2.basetable3.basetable6.Any(h=>h.contact != null)
                            ? lev.basetable2.basetable3.basetable6.Where(h=>h.contact != null).Select(h=>h.contact).Distinct() : null),
        Key1 = lev.basetable5 != null ? lev.basetable5.Id : null,
        Key2 = lev.basetable2 != null && lev.basetable2.basetable3 != null && lev.basetable2.basetable3.basetable4 != null ?
                            lev.basetable2.basetable3.basetable4.id      : null

    })
    .Distinct()
    .ToList();

foreach (var x in tempResultSet)
{
    if (x.Contacts != null)
    {
        foreach (var contact in x.Contacts)
        {
            result.Add(new ResultModel
            {
                Key1 = x.Key1,
                Key2 = x.Key2,                           
                ContactKey = contact.id
            });
        }
    }
    else
    {
        result.Add(new ResultModel
        {
            Key1 = x.Key1,
            Key2 = x.Key2                        
        });
    }
}

return result;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use SelectMany to flatten the array, e.g.:

List<ResultModel> result = _context.MainRecordTable.Where(h => h.id)
    .Select(lev => new 
    {
        Contacts = (lev.basetable2 != null 
                            && lev.basetable2.basetable3 != null 
                            && lev.basetable2.basetable3.basetable6 != null
                            &&  lev.basetable2.basetable3.basetable6.Any(h=>h.contact != null)
                            ? lev.basetable2.basetable3.basetable6.Where(h=>h.contact != null).Select(h=>h.contact).Distinct() : null),
        Key1 = lev.basetable5 != null ? lev.basetable5.Id : null,
        Key2 = lev.basetable2 != null && lev.basetable2.basetable3 != null && lev.basetable2.basetable3.basetable4 != null ?
                            lev.basetable2.basetable3.basetable4.id      : null

    })
    .Distinct()
    .ToList()
    .SelectMany(x => {
      if (x.Contacts == null)
        return new ResultModel[]  
        {
          Key1 = x.Key1, 
          Key2 = x.Key2,
        };
      else 
        return x.Contacts.Select(contact => new ResultModel() 
          { 
            Key1 = x.Key1, 
            Key2 = x.Key2, 
            ContactKey = contact.id,
          }).ToArray();        
    })
    .ToList();

In the SelectMany, it is checked whether the contact list is set; if not, a single item is returned, otherwise, an item for each contact is returned.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading