【发布时间】:2020-02-18 23:03:41
【问题描述】:
我有那个类型的列表:
IEnumerable<Resource> ResourceAuthorizedForLoggedUser= resourceAuthorizer.FindAll();
先试试这个,但没用:
var authorizations= ResourceAuthorizedForLoggedUser.Select(x => x.Id).ToString();
而且我需要放入那个 Session:
System.Web.HttpContext.Current.Session["Authorized_List"] = authorizations;
为了能够在基本控制器中访问这些数据
public class BaseController : Controller
{
public static List<string> authorizations { get { return Getauthorizations(); } }
private static List<string> Getauthorizations()
{
List<string> authorizationsList = (List<string>)System.Web.HttpContext.Current.Session["Authorized_List"];
return authorizationsList != null && authorizationsList.Any() ? authorizationsList : new List<string>();
}
}
最后,Getauthorizations 方法出现以下错误:
无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1 [System.String]”类型的对象
有什么想法可以转换该列表吗?
【问题讨论】:
-
ToList()而不是ToString()在设置会话变量时。 -
ResourceAuthorizedForLoggedUser.Select(x => x.Id)只会获得一个用户的可能性更大,为什么要列出它?在System.Web.HttpContext.Current.Session["Authorized_List"] = authorizations;上设置断点authorizations是什么类型,不是你想的那样。 -
@Çöđěxěŕ 我原以为
resourceAuthorizer.FindAll()可以返回多个对象。 -
@JohnathanBarclay 确实如此,但看看
ResourceAuthorizedForLoggedUser.Select(x => x.Id).ToString()... 这不会提供集合:) -
@Çöđěxěŕ 是的,因此是我最初的评论;应该是
.Select(x => x.Id).ToList()。
标签: c# asp.net-mvc session