Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to use studIDAbstract in another page but it is declared and initialized in the loop. but it doesn't allow as it say not the current coontext.

What I have tried:

C#
public object GetStuCosineSimilarity()
{
         
   for(int i =0; i<array.Length; i++)
   {
        int a = array[i];
        var studUserId = (from s in DB.Students 
                          where s.StudentId == a 
                          select s.UserId).FirstOrDefault();
        var studIDAbstract = (from u in DB.Users
                              join stud in DB.Students on u.UserId equals stud.UserId
                              where u.UserId == studUserId
                              select new
                              {
                                FirstName = u.FirstName,
                                Email = u.Email
                              }).ToList();
                
    }
   return studIDAbstract;
}
Posted
Updated 2-May-16 2:34am
v2

1 solution

Create a Class with the properties as

C#
public class StudAbstract
       {
           public string FirstName { get; set; }
           public string Email { get; set; }
       }


and change your code as

C#
public List<studabsttract> GetStuCosineSimilarity()  // return a know type instead of anonymous data
       {
           List<studabstract> lst = new List<studabstract>();  // collection to hold all data in the loop

           for (int i = 0; i < array.Length; i++)
           {
               int a = array[i];
               var studUserId = (from s in DB.Students where s.StudentId == a select s.UserId).FirstOrDefault();
               var studIDAbstract = (from u in DB.Users
                                     join stud in DB.Students on u.UserId equals stud.UserId


                                     where u.UserId == studUserId
                                     select new StudAbstract()  // cast to a known type
                                     {
                                         FirstName = u.FirstName,
                                         Email = u.Email


                                     }).ToList();
               lst.AddRange(studIDAbstract);  // add the current data in the loop to a List

           }
           return lst;
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900