Hi
How to initialize Result variable
C#
Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students
where t2.MentorID == mentorId
select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }).Distinct().ToList();Thanks

Jignesh KumarRank #91 · 22.9K reputationPosted Apr 6, 2023, 11:46 PM EDT
Hello Ramco,
you can create one class with same property you want use in select statement, for above you can create,
Public class StudentDetails { public int StudentID {get;set;} public string Name {get;set;} public strin MobileNumber{get;set;} }You can initialize as below
var Result = new List(); Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students where t2.MentorID == mentorId select new StudentDetails{ studentId = t2.StudentID, Name = t2.Name, MobileNumber = t2.MobileNo}).Distinct().ToList();Then in UI/ Cshtml filr you can do string concate using Name + MobileNumber
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 9:33 AM EDT
List Result = new List(); Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students where t2.MentorID == mentorId select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }).Distinct().ToList();Ramco RamcoRank #462 · 5.4K reputationPosted Apr 6, 2023, 9:45 AM EDT
Hi Rajkiran
I am getting error cannot convert from system.collection.generic to system.collection.dynamic.
Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students where t2.MentorID == mentorId select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }).Distinct().ToList();Thanks
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 9:57 AM EDT
you need to create a new instance of the "StudentsBreak" class for each item in your LINQ query result and add it to a new list of type "List" . You can use a foreach loop to iterate over the items in the query result and create new instances of the "StudentsBreak" class for each item, like this:
List Result = new List(); foreach (var item in queryResult) { StudentsBreak student = new StudentsBreak(); student.StudentID = item.StudentID; student.Name = item.Name; Result.Add(student); }In this code, you would replace "queryResult" with your LINQ query result, and "StudentsBreak" with the name of your class that has the "StudentID" and "Name" properties. This will create a new list of "StudentsBreak" objects with the data from your query result.
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 9:57 AM EDT
The error "cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'" is caused by a mismatch between the type of the result of your query and the type of the variable you are trying to assign it to.
Ramco RamcoRank #462 · 5.4K reputationPosted Apr 6, 2023, 10:08 AM EDT
Hi Rajkiran
What should be type of Result . How it should be initialized.
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students where t2.MentorID == mentorId select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }).Distinct().ToList();Thanks
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 10:17 AM EDT
the type of Result in this case is an anonymous type, as it is created using the
new { }syntax in theselectstatement. The properties of this anonymous type areStudentIDandName.