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 JayaramanPosted Jun 27, 2013, 7:07 AM
{
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;
}
JosephsPosted Jul 3, 2013, 10:15 AM
Iftikar Hussain good post
vidhyaPosted Jun 27, 2013, 8:53 AM
I followed santhosh code to resolve that issue. thanks a lot santhosh.
Iftikar HussainPosted Jun 27, 2013, 7:10 AM
{
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.
vidhyaPosted Jun 27, 2013, 7:00 AM
vidhyaPosted Jun 27, 2013, 6:50 AM
Santhosh Kumar JayaramanPosted Jun 27, 2013, 6:49 AM
Iftikar HussainPosted Jun 27, 2013, 6:41 AM
Regards,
Iftikar
vidhyaPosted Jun 27, 2013, 6:27 AM
}
Santhosh Kumar JayaramanPosted Jun 27, 2013, 5:37 AM
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
VulpesPosted Jun 27, 2013, 5:33 AM
vidhyaPosted Jun 27, 2013, 5:26 AM
Person2 class contains firstname,lastname fields
Public list
but i need to two class fields. how do i fetch these 2 class field values.
Iftikar HussainPosted Jun 27, 2013, 5:19 AM
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
Santhosh Kumar JayaramanPosted Jun 27, 2013, 5:07 AM
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.
VulpesPosted Jun 27, 2013, 5:06 AM
If you want to return something to outside code, then create a 'named' class and use that instead of the anonymous type.
vidhyaPosted Jun 27, 2013, 5:04 AM
I get same error when i implement your code in my application.
it was shown in above reply.
vidhyaPosted Jun 27, 2013, 4:56 AM
it does not show error,
when i implement your code in application i got this error.
Cannot implicitly convert type 'System.Collections.Generic.List
Iftikar HussainPosted Jun 27, 2013, 4:44 AM
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.
Santhosh Kumar JayaramanPosted Jun 27, 2013, 4:41 AM
have you tried mine?
vidhyaPosted Jun 27, 2013, 4:32 AM
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 JayaramanPosted Jun 27, 2013, 2:35 AM
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};
Iftikar HussainPosted Jun 27, 2013, 2:14 AM
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.