Question

I have three tables Corevalue, SubjectType and Question.

I want to select CoreValue.Sname, SubjectType.Cname and Question.QuestionText, I know how it works with SQL but not LINQ any help would be appreciated.

Somethng like this in SQL:

SELECT 
   CoreValue.Cname, 
   Question.Questiontext, 
   SubjectType.Sname 
FROM 
   Corevalue 
JOIN Question
   ON Corevalue.CID = question.QID 
JOIN SubjecType 
   ON Question.QID = SubjectType.SID;

I quess this one is right, but I want to LINQ :/

Thanks on Advance

Best Regards!

Was it helpful?

Solution

You just need to express the join in SQL too:

var query = from core in db.Cores
            join question in db.Questions on core.CID equals question.QID
            join subject in db.Subjects on question.QID equals subject.SID
            select new {
                core.CoreName,
                question.QuestionText,
                subject.SubjectName
            };

Of course if you've mapped your database relations, you may not need to perform the join explicitly, preferring simple properties instead - it all depends on how you're using LINQ.

Note that your joins seem slightly odd, in terms of the names of the IDs - it's as if you've really got three entities all using exactly the same IDs. I'd expect something more like this:

var query = from core in db.Cores
            join question in db.Questions on core.ID equals question.CoreID
            join subject in db.Subjects on core.ID equals subject.CoreID
            select new {
                core.CoreName,
                question.QuestionText,
                subject.SubjectName
            };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top