我怎样才能返回studidabstract,因为它不在当前的上下文中? [英] How can I return studidabstract as it is not in the current context?

查看:92
本文介绍了我怎样才能返回studidabstract,因为它不在当前的上下文中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个页面中使用studIDAbstract,但它在循环中声明并初始化。但它不允许,因为它说不是当前的共同文本。



我尝试过:



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:

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;
}

推荐答案

创建一个属性为
的类


Create a Class with the properties as

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





并将您的密码更改为





and change your code as

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;
       }


这篇关于我怎样才能返回studidabstract,因为它不在当前的上下文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆