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
Solved

Need help with LINQ, doing a double join.

Posted on 2008-10-22
2
1,101 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 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with a Crystal Report in my C#.Net application 2 26
JSON  parse help 7 40
Open a link in vb.net 2 16
reading excel file in .net 2 15
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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