下面是我的代码,使用LINQ to SQL,执行时我得到一个错误消息,说“对象引用未设置为对象的实例”。我加入了3个表Users,UsersinRoles和Roles.Userinroles是桥表。我使用连接来从两个表中检索数据,使用桥表我得到对象引用错误。关于Silverlight项目的Linq-SQL错误
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);
}
}
}
}
哪条线路导致错误? – GrandMasterFlush 2012-03-21 14:11:20
构建没有错误,执行时会打开并出现此错误,但没有明确显示行 – newuser1555 2012-03-21 15:01:03
让Visual Studio在抛出异常时中断,它将在一分钟内清除导致异常的原因。我敢打赌,你的'Collection'类不是犹太教。无论如何,什么样的弱类型是? .net通用集合是不是更好? – 2012-03-25 22:29:23