2

I have a LINQ query where I need to join 3 tables but it seems to be hanging on evaluating the expression.

Do you see any problems with this or any ideas why I would be hanging? VS2010 output window shows different threads exiting with 0.

Here is the LINQ expression:

   var results = (from drECO in dtECO.AsEnumerable() 
                    join drBOMTo8 in dtBOM.AsEnumerable() on drECO.Field<string>("PN8Dig") equals drBOMTo8.Field<string>("PN8Dig")

                     from drBOMFrom8 in dtBOM.AsEnumerable ()
                     join drFAA8 in dtFAA.AsEnumerable () on drBOMFrom8.Field<string>("PN8Dig") equals drFAA8.Field <string>("PN8Dig")

                     from drBOMFromMon in dtBOM .AsEnumerable ()
                     join drFAAMon in dtFAA .AsEnumerable () on drBOMFromMon.Field <string>("Mon") equals drFAAMon.Field <string>("Mon")


                      select new
                     {
                         PN = drECO .Field <string>("PN"),
                         PNNEWREV = drECO.Field <string>("PNNewRev"),
                         PN8DIG = drECO.Field <string >("PN8Dig"),
                         ECO = drECO .Field <string >("ECO"),
                         MON = drFAAMon.Field <string>("Mon"),
                         SUPNUM = drFAAMon.Field <string>("SupNum"),
                         URL = drFAAMon.Field <string>("URL")
                     }).Distinct ();
Liz Ravenwood
  • 73
  • 1
  • 8
  • 2
    `from drBOMmon in dtBOM.AsEnumerable()` would create Cartesian product. Is that intended? – Ivan Stoev Jan 17 '17 at 21:00
  • OMG. Thank you so much. I did not know that. (as a side, I'm a newbie, and this is my first question) I have dtECO that I want to link to dtBOM by PN8Dig and then I want dtBOM to link to dtURL with Mon. Does this logic create a cartesian join? I'd like them to all be inner joins. Thanks Ivan. – Liz Ravenwood Jan 17 '17 at 21:18
  • Yes it does create Cartesian product because it's not connected with the previous joins. To make it inner join, you have to relate it to either `drECO` or `drBOM8Dig`, e.g. instead of `from drBOMmon in ..` you would use `join drBOMmon in ... on ...` – Ivan Stoev Jan 17 '17 at 21:24
  • Yes @Ivan. I need dtECO to link to dtBom by 8DigPN, and dtBom to link to dtFAA by 8DigPN and by Mon. I've changed it but it is still hanging. – Liz Ravenwood Jan 18 '17 at 18:22
  • It would be nice if you update the question with what you did. Also what happens (just for the sake of test) if you replace `Distinct()` with `Count()` - does it complete and what is the result number? – Ivan Stoev Jan 18 '17 at 18:42
  • Hmm, with the update it's getting worse. See, the query should have **only one** `from` clause. Remove the second and third `from` lines and try to join the 3 tables. Something like `from t1 in T1 join t2 in T2 on t1.F1 equals t2.F2 join t3 in T3 on t2.F4 equals t3.F5`. – Ivan Stoev Jan 18 '17 at 18:59
  • This answers the "hanging" issue. I have duplicates, but that is another issue. Can you post this as an answer so I can mark it thus? – Liz Ravenwood Jan 18 '17 at 21:45

2 Answers2

0

In my experience, hanging and weird control flow issues with LINQ occur when the provider, it's driver or the underlying database the provider is talking to is having issues.

If you're using a database that can analyze executed queries, use that mechanism to see what happens at that layer. If it doesn't even get there, please add that information to your question.

Ani
  • 10,826
  • 3
  • 27
  • 46
0

The answer is in the comments section from initial question from @Ivan in that the "hanging" was because it was trying to perform a Cartesian join. Fixed the LINQ. "es it does create Cartesian product because it's not connected with the previous joins. To make it inner join, you have to relate it to either drECO or drBOM8Dig, e.g. instead of from drBOMmon in .. you would use join drBOMmon in ... on ..."

Liz Ravenwood
  • 73
  • 1
  • 8
  • Your should quote that comment into your answer, since that user could delete the comment, making your answer useless. – Adam Feb 23 '17 at 22:44