0

I have Inventories table and here I save all ProductId's who is below 10 or the same as 10 in Array

public int[] OrderNow()
{
    return context.Inventory.Where(u => u.Quantity <= 10).Select(i =>i.ProductId)
                                                         .ToArray();
}

I know I have 3 products that Quantity is below 10 and when I run debug I can see those Ids. So far so good.

But When I want to retrieve names of those products, I just can get one of those 3 products...

To get names of those Products I wrote this code, but it's returning only one Product name.

public string[] ProductName()
{
     var prId = OrderNow();
     foreach (var p in prId)
     {

       return context.Products.Where(u => u.ProductId == p)
                              .Select(p => p.ProductName).ToArray();
          // After one loop it's jumping foreach process .. I don't know why
     }
     return null; // I don't know what to write here, but I must to return something
}

Edited with a similar problem...

public int[] OrderNow()
{
    return context.Inventory.Where(u => u.Quantity <= 10).Select(i =>i.DepartmentId)
                                                         .ToArray();
}

And my User email

public string[] UserEmails()
{
   var departmentIds= OrderNow();   // Gets the array of product ids

   return context.Users
         .Where(u => u.IsInventoryAdmin == true)
                 .Where(u => departmentIds.Contains(u.DepartmentId)) // Here failing
                 .Select(e => e.Email)
                 .ToArray();    
}
4
  • return breaks out of a method. Commented Sep 30, 2017 at 13:01
  • return exits your function. And you are already returning string[] Commented Sep 30, 2017 at 13:02
  • @DanielA.White thank you for your response, What Should I write instead? Commented Sep 30, 2017 at 13:02
  • Best guess is you want just return context.Products.Where(u => prId.Contains(u.ProductId)).Select(p => p.ProductName).ToArray(); Commented Sep 30, 2017 at 13:04

1 Answer 1

1

Because you are returning inside the loop. So when the loop executes for the first item in the prId array, it gets only that product (Basically a collection with only that product) and since you have the return statement, it will be returned(exiting that code block).Your loop iteration will not be executed for the remaining items in the array.

You should use Contains method. This allows you to query the Products which has ProductId matching to items inside the array ( the productIds you got from OrderNow method)

public string[] ProductName()
{
   var productIds= OrderNow();   // Gets the array of product ids

   return context.Products
                 .Where(u => productIds.Contains(u.ProductId))
                 .Select(p => p.ProductName)
                 .ToArray();    
}
Sign up to request clarification or add additional context in comments.

8 Comments

Yea. Just fixed it. Thanks
@Shyju I get Error when I want to do allmost like you answered. This time I collected departmentId in Array from OrderNow() Then in my Asp.NetUsers I have departmentId, and emails. I wanted to collect ueser emails in Array who have the same departmentId. Its allmost the same if you change Products to Users in you answer. I get error int[] does not contain for definition for 'Contains' and the best extenstion method overload 'Queryable.Contains<int?> .....'
Add using System.Linq; to that class file.
@Shyju I have edited my original question in the bottom
try .Where(u => departmentIds.Any(u.DepartmentId))
|

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.