Solved

Need help with LINQ, doing a double join.

Posted on 2008-10-22
2
1,103 Views
Last Modified: 2013-11-11
Friends,

I am trying to create a LINQ qry in C# that has a double join; however, mine isn't quite right.

Here is the SQL syntax:

Declare @RunID int
Set @RunID =23

Select
r.No,
st.EntryTime,
st.ExitTime,
st.SectionID,
st.ShortName,
st.SectionSpeed,
st.SectionTime,
spo.SectionKey,
spo.SectionID,
st.Lap
From SectionTimes st inner join Results r
on st.RunID=r.RunID
AND st.ResultItemID=r.ResultID
inner join SectionPreferredOrder spo
on st.SectionID = spo.SectionID
WHERE st.RunID=@RunID and st.Lap < 65536
and st.shortname like 'S%'
and (st.Flag=1 or st.Flag=2 or st.Flag=4) and st.Lap >=0

And here is what I have for LINQ:

var query = from st in dc.Sectiontimes
join r in dc.Results on new { st.RunID, st.ResultID } equals new { r.RunID, r.ResultID }
join spo in dc.SectionPreferredOrders on new { st.SectionID } equals new { spo.SectionID1 }
where st.RunID == RunId && st.Lap < 65536 && st.ShortName.StartsWith("S") && (st.Flag == 1 || st.Flag == 2 || st.Flag == 4)                                              
&& st.Lap >= 0
orderby st.EntryTime, st.ExitTime, st.SectionID
select new
     {
          r.No,
          st.EntryTime,
          st.ExitTime,
          st.SectionID,
          st.ShortName,
          st.SectionSpeed,
          st.SectionTime1,
          st.Lap,
          spo.SectionID1,
          spo.SectionKey
        };

I however am getting an error on the line:
join spo in dc.SectionPreferredOrders on new { st.SectionID } equals new { spo.SectionID1 }

join is underlined and the error is:
Error      15      The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.

Any ideas how to fix it?

Thanks in advance!

Best Regards,
Eric
0
Comment
Question by:indy500fan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
novynov earned 500 total points
ID: 22782917
I may be wrong, but I believe the problem is that your join defines 2 anonymous types with no named property members. Although they have the same fundamental property types, the type inference engine is hanging up...I suspect if you looked at the disassembly for the anonymous types, you'd find that the compiler is assigning different prop names to each type....which then makes them two different types.

I simulated this scenario...and reproduced your results. I believe there are 2 solutions:

- Just get rid of the anonymous types on the second join. If you aren't joining on multiple equalities, you could just do "on  st.SectionID equals spo.SectionID1" You will need them in your first join...in which case, I think you may need to apply the next item to it as well.
or
- Add a property name to each anonymous type. For example, "on new {ID = st.SectionID} equals new {ID = spo.SectionID1}" I did this with my simulation, and it fixed things

Let me know if this addresses your issue.

0
 

Author Comment

by:indy500fan
ID: 22783137
The second scenario works for me.  Brilliant.  Thank you!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question