0

I am getting some errors in my controller.
At first, I got Suppliers List, then I got Id for all Suppliers, then I got all Users for every Supplier.

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<int> supplierIds = new List<int>();
        foreach (SupplierRow sr in suppliers)
        {
            supplierIds.Add(sr.Id);
        }

        var users = Context.Users.AsNoTracking()
            .Where(e => supplierIds.Contains(e.SupplierId))
            .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
            }).toList();

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

        return PartialView("_Grid", suppliers);
    }

here
enter image description here

and here enter image description here
What's wrong with my code? How to fix that?

1
  • 1
    To improve performance and scalability, you should try to combine the two queries into one that is joining the tables. Commented Jun 6, 2013 at 10:26

1 Answer 1

1

The problem is that you are trying to add Guid object to a collection that only accepts int values. Your User.SupplierId is an object of type Guid? (or Nullable<Guid>), while Supplier.Id is Guid. Fix the collection by declaring it as:

List<Guid> supplierIds = new List<Guid>();

Then in you code use:

foreach(SupplierRow sr in suppliers)
{
    supplierIds.Add(sr.Id);
}

Do the same thing for users except that you will have to use SupplierId.HasValue and SupplierId.Value to check whether it has a value and to read the value. This is because it is declared as nullable Guid.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, I already have found the first error, but I dont understand what should I do with the second error?
The second error should fix itself if you change the declaration from List<int> to List<Guid> for supplierIds variable. The error message is telling you that method Contains expects int value.
I changed the declaration List<Guid> supplierIds = new List<Guid>(); but I continue to get the second error in .Where(e => supplierIds.Contains(e.SupplierId)) - some invalid arguments
Try .Where(e => supplierIds.Contains(e.SupplierId.Value)) e.SupplierId is Nullable<Guid> and .Value will return non-Nullable value.
It should be .ToList (with capital T)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.