I need to convert sql query into linq. I have placed my query for your reference.
select p.firstname,p.lastname,a.assignmentname,
a.makeavailable,a.pointspossible,a.duedatetime
from person2 p
inner join coursesectionroster cs
on cs.personid = p.personid
inner join assignmentsubmission asn
on asn.coursesectionrosterid = cs.coursesectionrosterid
inner join assignment a
on a.assignmentid = asn.assignmentid
where cs.coursesectionid='b78a6efe-ac77-4e49-806a-fc2fad71068b'
and cs.courserole=2
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 27, 2013, 3:07 AM EDT
{
int roler = (int)CourseSectionRoster.CourseRoleTypes.Student;
List<StudentGradeBook> assignmentList=new List<StudentGradeBook> ();
var q =
from p in _context.Person2
join cs in _context.CourseSectionRoster on p.PersonId equals cs.PersonId
join asn in _context.AssignmentSubmission on cs.CourseSectionRosterId equals asn.CourseSectionRosterId
join a in _context.Assignments on asn.AssignmentId equals a.AssignmentId
where cs.CourseSectionId == courseSectionId && cs.CourseRole == roler
select new {p.FirstName , p.LastName , a.AssignmentName , a.MakeAvailable , a.PointsPossible , a.DueDateTime };
foreach(var item in q)
{
StudentGradeBook a=new StudentGradeBook(){FirstName=item.FirstName,
LastName=item.LastName,
AssignmentName=item.AssignmentName,
MakeAvailable=item.MakeAvailable,
PointsPossible =item.PointsPossible ,
DueDateTime=item.DueDateTime
};
assignmentList.Add(a);
}
return assignmentList;
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jun 26, 2013, 10:14 PM EDT
Here it is
var result = from p in db.person2
Let me know if you need more information
Regards,
Iftikar
Remember to click "Mark as Answer" on the post, if it helps you.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 26, 2013, 10:35 PM EDT
var q =
from p in person2
join cs in coursesectionroster on p.personid equals cs.personid
join asn in assignmentsubmission on cs.coursesectionrosterid equals asn.coursesectionrosterid
join a in assignment on asn.assignmentid equals a.assignmentid
where cs.courserole==2 && cs.coursesectionid==Guid.Parse("b78a6efe-ac77-4e49-806a-fc2fad71068b")
select new {p.firstname,p.lastname,a.assignmentname,a.makeavailable,a.pointspossible,a.duedatetime};
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 12:32 AM EDT
It shows error in the line
join cs in _context.CourseSectionRoster on cs.personid equals p.personid
cs is not scope of the right side equals for p also it shows this error.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 27, 2013, 12:41 AM EDT
have you tried mine?
Iftikar HussainRank #423 · 5.9K reputationPosted Jun 27, 2013, 12:44 AM EDT
sorry, Please check now
var result = from p in db.person2
Regards,
Iftikar
Remember to click "Mark as Answer" on the post, if it helps you.
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 12:56 AM EDT
it does not show error,
when i implement your code in application i got this error.
Cannot implicitly convert type 'System.Collections.Generic.List
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 1:04 AM EDT
I get same error when i implement your code in my application.
it was shown in above reply.
VulpesRank #55 · 29.2K reputationPosted Jun 27, 2013, 1:06 AM EDT
If you want to return something to outside code, then create a 'named' class and use that instead of the anonymous type.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 27, 2013, 1:07 AM EDT
foreach(var item in q)
{
Assignment a=new Assignment(){FirstName=item.FirstName,
LastName=item.LastName,
AssignmentName=item.AssignmentName,
MakeAvailable=item.MakeAvailable,
PointsPossible =item.PointsPossible
};
assignmentList.Add(a);
}
This should work.
The issue was we are getting new anonymous type with selected columns from different table. So at the end, you have to assign these columns to the properties of Assignment Class.
I am not sure about the names of the properties in Assignment Class. So i used same name. Modify the names as you have in your class to make it work.
Iftikar HussainRank #423 · 5.9K reputationPosted Jun 27, 2013, 1:19 AM EDT
var result = from p in db.person2
{
FirstName=p.firstname,
LlastName=p.lastname,
Assignmentname=a.assignmentname,
MakeAvailable=a.makeavailable,
PointsPossible=a.pointspossible,
DueDatetime=a.duedatetime
var assignments= new List<Assignment>(result.ToList());
Regards,
Iftikar
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 1:26 AM EDT
Person2 class contains firstname,lastname fields
Public list
but i need to two class fields. how do i fetch these 2 class field values.
VulpesRank #55 · 29.2K reputationPosted Jun 27, 2013, 1:33 AM EDT
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 27, 2013, 1:37 AM EDT
If you are just going to return type of Assignment , then you no need to include first name and lastname, right?
If you want to return mix of both, then as Vulpes said, create custom class PersonAssignment with all the properties you wanted and use that afterwards
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 2:27 AM EDT
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jun 27, 2013, 2:41 AM EDT
Regards,
Iftikar
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jun 27, 2013, 2:49 AM EDT
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 2:50 AM EDT
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 3:00 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jun 27, 2013, 3:10 AM EDT
{
FirstName=p.firstname,
LlastName=p.lastname,
AssignmentName=a.assignmentname,
MakeAvailable=a.makeavailable,
PointsPossible=a.pointspossible,
DueDateTime=a.duedatetime
var studentGradeBooks= new List<StudentGradeBook>(result.ToList());
Regards,
Iftikar
Remember to click "Mark as Answer" on the post, if it helps you.
vidhyaRank #4,868 · 321 reputationPosted Jun 27, 2013, 4:53 AM EDT
I followed santhosh code to resolve that issue. thanks a lot santhosh.
JosephsRank #1,506 · 1.1K reputationPosted Jul 3, 2013, 6:15 AM EDT
Iftikar Hussain good post
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts