Solved

Need help with LINQ, doing a double join.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now