0

这是我的代码。我想将列表保存在会话变量中以供以后进行身份验证(它是用户有权访问的对象列表......)我收到一条错误消息,指出它无法将 System.Collections.Generic.List 隐式转换为'System.集合.通用.列表。帮助?

    protected void Session_Start(object sender, EventArgs e)
    {
        string strUserName = User.Identity.Name;
        string strShortUserName = strUserName.Replace("WINNTDOM\\", string.Empty).ToUpper();
        System.Web.HttpContext.Current.Session["strShortUserName"] = strShortUserName;
        System.Web.HttpContext.Current.Session["strUserName"] = strUserName;
        List<string> authorizedObjects = new List<string>();
        using (CPASEntities ctx = new CPASEntities())
        {
            var w = (from t in ctx.tblUsers
                     where (t.UserPassword == strUserName)
                     select t).FirstOrDefault();
            if (!(w==null))
            {
                authorizedObjects = (from t in ctx.qryUserObjectAuthorization
                                         where (t.UserPassword == strUserName)
                                         select new {  n = t.ObjectName }).ToList();

            }
        }
    }
4

3 回答 3

1

authorizedObjectsList<string>,但您试图将其用作匿名类型的列表:

List<string> authorizedObjects = new List<string>();

(...)

authorizedObjects = (from t in ctx.qryUserObjectAuthorization
                     where (t.UserPassword == strUserName)
                     select new {  n = t.ObjectName, i = t.ObjectID }).ToList()

将您的查询更改为:

authorizedObjects = (from t in ctx.qryUserObjectAuthorization
                     where (t.UserPassword == strUserName)
                     select t.ObjectName).ToList()
于 2013-02-28T17:20:18.397 回答
1

您正在初始化一个List<string>对象但填充不同的对象。

List<string> authorizedObjects = new List<string>();

select new {  n = t.ObjectName, i = t.ObjectID } <--construct a class with these properties and initialize `List<WithNewClassName>`
于 2013-02-28T17:21:56.557 回答
1

要将其生成为字符串列表,请使用

authorizedObjects = (from t in ctx.qryUserObjectAuthorization
                     where (t.UserPassword == strUserName)
                     select t.ObjectName).ToList();
于 2013-02-28T17:22:20.427 回答