Silverlight项目上的Linq-SQL错误

时间:2012-03-21 14:08:13

标签: sql-server-2008 entity-framework-4

下面是我使用LINQ to SQL的代码,在执行时我得到一个错误消息“对象引用未设置为对象的实例”。我已加入3个表Users,UsersinRoles和Roles.Userinroles是桥表.I使用join来使用桥表从2个表中检索数据我得到了Object引用错误。

 public class Users : CollectionFactoryBase
{
    public Users()
    {
        this.Summary = "Collection of Users";
    }
    public override Collection MakeCollection(CollectionRequestContext context)
    {
        UsersDataContext m_dataContext = new UsersDataContext();

        const int maxItems_c = 150;
        try
        {
           // string sessionvalue = HttpContext.Current.Session["SessionKey"] as string;

            var Users = from p in m_dataContext.aspnet_Users
                        join t in m_dataContext.aspnet_UsersInRoles on
                        p.UserId equals t.UserId
                        join r in m_dataContext.aspnet_Roles on
                        t.RoleId equals r.RoleId 

                        select new
                        {
                            UserName = p.UserName,
                            UserId   = p.UserId,
                            RoleId   = r.RoleId,
                            RoleName = r.RoleName,
                           userid = t.UserId,
                           roleid = t.RoleId


                        };


            Collection collection = new Collection();
            collection.Name = "Users";
            foreach (var user in Users.Take(maxItems_c) )
            {
                collection.AddItem(user.UserName, user.RoleName, null, null, null, null, null);
            }
            return collection;
        }
        catch (Exception ex)
        {

            return ErrorCollection.FromException(ex);
        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

难道你不认为最后一句话应该是第一个吗?