locked
Shorter Readable query in LINQ RRS feed

  • Question

  • User546174381 posted

    Hello, I have created 2 queries that yield the same results, one is not very readable but is shorter:

    var ppls = db.ProjectMembers
    .Where(f => f.Project.ProjectStatusSet.Status == "Active" && f.IsContact == true)
                    .Select(z => z.Person).GroupBy(p => p.Id)
                    .Select(grp => grp.FirstOrDefault())
                    .OrderBy(t => t.Name)
                    .ToList();

    and the other is readable but longer:

     var ppls =
                    (from ppl in db.People
                     join pm in db.ProjectMembers
                         on ppl.Id equals pm.ProjectMember_Person
                     join p in db.Projects
                         on pm.ProjectMember_Project equals p.Id
                     join pss in db.ProjectStatusSets
                         on p.Project_ProjectStatus equals pss.Id
                     where pss.Status == "Active" && pm.IsContact == true
                     select ppl
                    ).GroupBy(p => p.Id).Select(grp => grp.FirstOrDefault()).OrderBy(t => t.Name).ToList();

    How can I make this readable and concise?

    Saturday, April 14, 2018 12:20 AM

All replies

  • User1120430333 posted

    You should be happy that you got them both to work.  What difference does it make that one is using lambda fully that makes the statement shorter as opposed to the other statement using a combination long-hand and lambda? They are both readable in my view.   

    Saturday, April 14, 2018 10:31 PM