Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with LINQ, doing a double join.

Posted on 2008-10-22
2
Medium Priority
?
1,104 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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