I am rewriting a VB.NET app into C#. I won't subject you to the original code. I am mainly looking for a better way to handle all the join statements that are being done. I am dealing with a legacy database that I cannot currently change the structure of. (Though I am making my arguments for it)

I am wondering if there is a better / cleaner way to handle multiple join statements. I am using DB first EF 6.1 so I have more options than was originally available. Here is the original Linq query converted to C#:

(from myObject in _context.MyObjects
join myObjectType in _context.MyObjectTypes on myObject.CRTKey equals myObjectType.CRTKey
join myObjectsSchedule in _context.MyObjectSchedule on myObject.CRS_Key equals myObjectsSchedule.CRS_Key
join myObjectGroup in _context.MyObjectGroups on myObject.ReportKey equals myObjectGroup.ReportKey
where myObjectGroup.ReportGroupNameKey == groupNameKey
select myObject).Distinct();

This particular example only has three joins I have a few blocks of code that have in excess of 6 join statements. Is there a better way to handle all the join statements?

  • \$\begingroup\$ Keep in mind, that each join-query has their own properties. We will most probably not be able to give advice on things you didn't show us :( \$\endgroup\$
    – Vogel612
    Jul 31, 2014 at 12:26
  • \$\begingroup\$ @Vogel612 I can post all of the objects in this query. I was mainly looking for a "better practice" or "rule of thumb" for dealing with multiple join statements. \$\endgroup\$
    – Robert
    Jul 31, 2014 at 12:29
  • \$\begingroup\$ that's up to you to decide. keep in mind that anything you post here is under CC-BY-SA license. So anyone can use it... General rule of thumb: the more info you give, the more info you may get \$\endgroup\$
    – Vogel612
    Jul 31, 2014 at 12:32
  • \$\begingroup\$ What's the purpose of myObjectType and myObjectsSchedule when you never use them? This doesn't look like real code to me, so I think the question is off-topic, unless you fix that. \$\endgroup\$
    – svick
    Aug 2, 2014 at 1:27
  • \$\begingroup\$ @svick It is not meant to be "real code" I posted a sanitized version of my code. The code itself is not important to the question at hand in this example. My question was directly related to any linq statement with many joins. I feel that this question is on topic due to that fact. \$\endgroup\$
    – Robert
    Aug 4, 2014 at 12:09

1 Answer 1


I'm not sure what you mean by a 'cleaner way'. The syntax you're using is known as query syntax and is by far the best for readability when you have multiple joins.

If you use method syntax to express the same query you'll quickly see what I mean.

However, if you use method syntax you'll be able to break those joins up using different methods.

For example:

IGrouping<Results> AddJoin(IQueryable<Stuff> query) {

    if (business_rule) {
        return query.Join(inner => inner.Key, outer => inner.Key == outer.Key);

    return query;

In this way you can reuse parts of your join and construct them dynamically.

Hope this helps.

  • \$\begingroup\$ I was looking for a way to reduce the amount of code per method. On that note this is a really good idea. I don't need dynamically created queries but I will use this to clean up the methods that I have. \$\endgroup\$
    – Robert
    Jul 31, 2014 at 12:57
  • \$\begingroup\$ That won't compile, IQueryable<Stuff> is not IGrouping<Results>. \$\endgroup\$
    – svick
    Aug 2, 2014 at 1:15
  • \$\begingroup\$ It's not meant to compile, it's meant to be exemplary. \$\endgroup\$
    – Razor
    Aug 2, 2014 at 9:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.