2
\$\begingroup\$

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();
        account.Dump();
    }
}

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;
    }
}
\$\endgroup\$

closed as off-topic by t3chb0t, hjpotter92, Raystafarian, Mast, Imus Apr 9 '18 at 11:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, hjpotter92, Raystafarian, Imus
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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