Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with LINQ, doing a double join.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

772 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