0

我的项目中有供应商模型和用户模型,每个供应商都有几个用户
供应商模型

public class SupplierRow
{
    public Guid Id { get; set; }
    public string FullName { get; set; }
    public bool Subscribed { get; set; }
    public bool Active { get; set; }
    public int Visits { get; set; }

    public List<UserRow> Users { get; set; }

    public bool AllInactive
    {
        get
        {
            foreach (UserRow ur in Users)
            {
                if (ur.Status == 1) return false;
            }
            return true;
        }
    }
}

和用户模型

public class UserRow
{
    public Guid Id { get; set; }
    public string FullName { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Status { get; set; }
    public int Role { get; set; }

    public Guid SupplierId { get; set; }
}

然后我在控制器中使用我的模型

 public ActionResult Grid(bool? active)
    {
        var suppliers = Context.Suppliers.AsNoTracking()
            .WhereIf(active != null, e => e.Active == active)
            .Select(e => new SupplierRow
                            {
                                Id = e.Id,
                                FullName = e.FullName,
                                Active = e.Active,
                                Visits = e.Visits,
                            })
                            .ToList();


        List<Guid> supplierIds = new List<Guid>();
        foreach (SupplierRow sr in suppliers)
        {
            supplierIds.Add(sr.Id);
        }

        var users = Context.Users.AsNoTracking()
            .Where(e => supplierIds.Contains(e.SupplierId.Value))
            .Select(e => new UserRow
            {
                Id = e.Id,
                FullName = e.FullName,
                Email = e.Email,
                Name = e.Name,
                Status = e.Status,
                Role = e.Role,
                SupplierId = e.SupplierId.Value
            }).ToList();


        foreach (UserRow ur in users) 
        {
            foreach (SupplierRow sr in suppliers) 
            {
                if (ur.SupplierId == sr.Id) 
                {
                    sr.Users.Add(ur);
                } 
            }
        }

        return PartialView("_Grid", suppliers);
    }

但是当我尝试调试我的项目时,我在这里遇到了一些异常有 在此处输入图像描述 什么问题?我该如何解决?

4

1 回答 1

2

您的Users列表未初始化。在访问它之前创建一个新列表Users = new List<UserRow>();您可以更改SupplierRow类:

public class SupplierRow {
    private List<UserRow> users = new List<UserRow>();

    public List<UserRow> Users
    {
        get { return users; }
        set { users = value; }
    }
    ...
}

或在构造函数中:

public class SupplierRow
{
    public SupplierRow()
    {
        Users = new List<UserRow>();
    }
    public List<UserRow> Users { get; set; }
    ...
}

或在访问它之前:

foreach (UserRow ur in users) 
{
    foreach (SupplierRow sr in suppliers) 
    {
        sr.Users = new List<UserRow>();
        if (ur.SupplierId == sr.Id) 
        {
            sr.Users.Add(ur);
        } 
    }
}

或者你可以只使用 linq:

foreach (SupplierRow sr in suppliers) 
{
    sr.Users = users.Where(user => user.SupplierId == sr.Id);
}

return PartialView("_Grid", suppliers);
于 2013-06-07T06:32:27.687 回答