Cannot get LINQ statement to work
-
Dear All I have the following linq statement var IncompleteMonitoringReportsCount = (from l in Logons join con in Consultants on l.IDCounter equals con.IDContact join tb in Topic on con.CountryCode equals tb.CountryCode join tx in TopicText on new {tb.IDTopic, tb.BaseLanguageCode} equals new {tx.IDTopic, tx.LanguageCode} select l).Count(); What Am I doing wrong in this query? Thanks for your help and time
-
Dear All I have the following linq statement var IncompleteMonitoringReportsCount = (from l in Logons join con in Consultants on l.IDCounter equals con.IDContact join tb in Topic on con.CountryCode equals tb.CountryCode join tx in TopicText on new {tb.IDTopic, tb.BaseLanguageCode} equals new {tx.IDTopic, tx.LanguageCode} select l).Count(); What Am I doing wrong in this query? Thanks for your help and time
This is what you're doing wrong:
jmonfu wrote:
new {tb.IDTopic, tb.BaseLanguageCode} equals new {tx.IDTopic, tx.LanguageCode}
The two new statements will generate totally different objects which are not equal. Why cant you just do:
join tx in TopicText on (tb.IDTopic equals tx.IDTopic and tb.BaseLanguageCode equals tx.LanguageCode)
(untested - give it a try!)
-
Dear All I have the following linq statement var IncompleteMonitoringReportsCount = (from l in Logons join con in Consultants on l.IDCounter equals con.IDContact join tb in Topic on con.CountryCode equals tb.CountryCode join tx in TopicText on new {tb.IDTopic, tb.BaseLanguageCode} equals new {tx.IDTopic, tx.LanguageCode} select l).Count(); What Am I doing wrong in this query? Thanks for your help and time
Try this: from l in Logons join con in Consultants on l.IDCounter equals con.IDContact join tb in Topic on con.CountryCode equals tb.CountryCode from tx in TopicText where tx.IDTopic == tb.IDTopic && tx.LanguageCode == tb.BaseLanguageCode select l I think the problem is that the LINQ parser is looking at the equality of two anonymous objects (your 'new {..}'s) via GetHashCode, and not the properties of those declared anonymous objects.
-
This is what you're doing wrong:
jmonfu wrote:
new {tb.IDTopic, tb.BaseLanguageCode} equals new {tx.IDTopic, tx.LanguageCode}
The two new statements will generate totally different objects which are not equal. Why cant you just do:
join tx in TopicText on (tb.IDTopic equals tx.IDTopic and tb.BaseLanguageCode equals tx.LanguageCode)
(untested - give it a try!)
As far as I know, the LINQ 'join' can only use one equality comparer. For complex joins you should use multiple 'from's. Example: from n in Logins from z in Topics where n.ID==z.ID && n != null select n