-2

i am trying to get data from database passing a string value. but get null value instead of the data. i have tried the following code

order getCustomerOrder(string or_n)
        {
            using (foodorderingEntities db = new foodorderingEntities ())
            {
                var result = db.orders.Where(or => or.order_no == or_n).FirstOrDefault();
                return result;
            }
        }

please some one guide me to solve this problem.

  • Leading/trailing spaces? Case differences? Not looking at the right database? Etc. Not much we can say with this tiny bit of info. – Gert Arnold Apr 03 '20 at 19:23

1 Answers1

0

Please paste your order object, so we know if its reference object/primitive etc, you can use this to understand the two ways to retrieve orders.

I would recommend you read this answer & this MSDN string compare, if your default culture is causing an issue in the comparison, it will help you understand whats going on

Options 1:

// Query syntax
IEnumerable<CustomerOrder> queryResultsCustomerOrder =
    from order in orders
    where order.number == myOrderNumber
    select order;

Options 2:

// Method-based syntax
IEnumerable<CustomerOrder> queryResultsCustomerOrder2 = orders.Where(order => order.Number == myOrderNumber);

using the above, now you can get however many orders the customer has. I am assuming your order is a number, but you can change it to whatever like a string.

Int based order comparison sample

 IEnumerable<CustomerOrder> getCustomerOrder(int myOrderNumber)
    {
       if(myOrderNumber <1) return null;

        using (foodorderingEntities dbContextOrderSet = new foodorderingEntities())
        {
            IEnumerable<CustomerOrder> resultsOneOrManyOrders = orders.Where(order => order.Number == myOrderNumber);
            return resultsOneOrManyOrders ;
        }
    }

string based order comparison

 IEnumerable<CustomerOrder> getCustomerOrder(string myOrderNumber)
    {
       //no orders
       if(String.IsNullOrEmpty(myOrderNumber)) return null;

        using (foodorderingEntities dbContextOrderSet = new foodorderingEntities())
        {
            IEnumerable<CustomerOrder> resultsOneOrManyOrders = orders.Where(order => order.Number == myOrderNumber);
            // you can replace the *** comparison with .string.Compare and try inside the block

            return resultsOneOrManyOrders ;
        }
    }
Transformer
  • 6,963
  • 2
  • 26
  • 52
  • No, the query is translated into SQL. Adding string.Compare with comparison options isn't supported. For the same reason, the client culture doesn't affect the comparison. It's the database collation that matters. – Gert Arnold Apr 03 '20 at 19:17