I wrote this in LinqPad. It works. The whole point was to retrieve header records, and only include some detail records. I'm looking for possible improvements to this, specifically around the anonymous type part. The only way I was able to get this to work as to use an anonymous type, then create the ToEntitySet() extension method to convert the detail rows. I just feel like I'm making this harder than it is. But maybe not? Any comments are welcome.

void Main()
    var query =
        from header in AccountHeader
        join detail in AccountDetail 
            on header.AccountHeaderId equals detail.AccountHeaderId
            into groupedDetails
        where groupedDetails.Any(x => x.Status == "status2")
        select new
            Account = header,
            AccountDetail = groupedDetails.Where(x => x.Status == "status2")

    foreach (var item in query)
        var account = item.Account;
        account.AccountDetail = item.AccountDetail.ToEntitySet();

public static class Extensions
    public static EntitySet<T> ToEntitySet<T> (this IEnumerable<T> source) where T : class
        var entitySet = new EntitySet<T> ();
        entitySet.AddRange (source);
        return entitySet;
  • 2
    \$\begingroup\$ You need to unfoo your code and post the real one. Currently this is off-topic. \$\endgroup\$ – t3chb0t Apr 7 '18 at 20:08
  • 1
    \$\begingroup\$ Why do I need to unfoo my code? How does that make a difference? \$\endgroup\$ – Bob Horn Apr 7 '18 at 20:17
  • 1
    \$\begingroup\$ Well, I'm stuck then, because my company doesn't want me to post our types. I'm really struggling to understand how it really matters. This is real code, with just a couple of types renamed. Does that really alter the overall understanding of what I've posted? \$\endgroup\$ – Bob Horn Apr 7 '18 at 20:35
  • 1
    \$\begingroup\$ Maybe try to find different names that still make sense and don't look like pseudocode... It's really hard to follow foos. We don't know your code and seeting only garbage isn't helping to understand it. \$\endgroup\$ – t3chb0t Apr 7 '18 at 20:40
  • 3
    \$\begingroup\$ That was sarcasm. \$\endgroup\$ – Jim Apr 7 '18 at 22:05