-2

i am getting an error on DuplicateOrder(TestOrder) as "cannot convert from tables.Order to int" even though the OrderID in the Orders table is an int

        void DuplicateOrder(int orderId)
        {
                string methodName = MethodBase.GetCurrentMethod().Name;
        try {
                using (MSBLegacyContext db = new MSBLegacyContext())
            {
                var TestOrder = db.Orders.Where(i=>i.OrderID == orderId).FirstOrDefault();
                DuplicateOrder(TestOrder);
            }
        }
        catch (Exception ex)
        {
Console.WriteLine(methodName, ex, string.Format("Test", orderId));
        }}

what am i missing here?

  • What are you trying to achieve? Notice that neither `Where` nor `FirstOrDefault` change the original type (collection of `tables.Order` will result in single (or null) instance of `tables.Order` being returned, hence you need to extract the `OrderId` property yourself. – orhtej2 Jul 24 '18 at 19:44
  • Should it be `DuplicateOrder(TestOrder.OrderId);` – Chad Jul 24 '18 at 19:47
  • 2
    @Chad that's the part I don't get, that's a recursive call with no stopper. – orhtej2 Jul 24 '18 at 19:48
  • Because your testOrder is of type Order and beware your method is recursive, would cause an infinite loop if there weren't an error. – Cetin Basoz Jul 24 '18 at 20:33
  • This code strongly indicates that you're missing a method `DuplicateOrder` having a parameter of type `Order`. This code can't be intended to be recursive. So the question is: where's the missing method? – Gert Arnold Jul 24 '18 at 20:58

2 Answers2

2

Root cause, the FirstOrDefault in below line will return the either first order object or null.

var TestOrder = db.Orders.Where(i=>i.OrderID == orderId).FirstOrDefault();

So type of testOrder is order, but the below method call is expecting the int parameter.

DuplicateOrder(TestOrder);

That's the reason yor are getting -

cannot convert from tables.Order to int

To fix the issue, you need to add Select query in the Linq query and select the int column, something like below -

var TestOrder = db.Orders.Where(i=>i.OrderID == orderId).Select(s=> s.YourIntColumn).FirstOrDefault();

Edit: After looking at you entire method and comments from others, you method call will definitely go to infinite loop. As you have not mentioned the purpose of this code, I am just assuming that you want to see whether a particular orderid is duplicate in the database or not. For that you can just use Count query and return the Boolean value. (not sure why do you have void in your method signature)

bool DuplicateOrder(int orderId) // changed from void to bool
{
  using (MSBLegacyContext db = new MSBLegacyContext())
  {
    return db.Orders.Where(i=>i.OrderID == orderId).Count()>1; // returns true if more than 1 order found   
  }
}
Vinit
  • 2,540
  • 1
  • 15
  • 22
2

If you have coded it correctly in your code, and if for a given orderId there were an Order, then your method would cause an infinite loop. Having said that, below is the correct way to get the int OrderId value:

using (MSBLegacyContext db = new MSBLegacyContext())
{
     var TestOrder = db.Orders
                    .FirstOrDefault(i=>i.OrderID == orderId);
     if (TestOrder != null)
     {
        var orderId = TestOrder.OrderId;
        // ...
     }
     // else whatever you would do
}

If OrderId is a primary key, then you would use SingleOrDefault() instead of FirstOrDefault(). The difference is, if there is more than 1 entry in the database with given orderId, SingleOrDefault() would throw an error - and in the case of primary keys that is what you want.

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39