Error trying to find record in simple table with Linq to Entities

I have a simple "Orders" table in which there are several entries. I am using code

  Order order;
        if (reg.OrderId != null)
        {
            order = db.Orders.Find(reg.OrderId);
        }
        else
        {
            order = new Order();
        }

to get an existing order from my table. reg.OrderId computes a prime integer (e.g. 55).

When I go through the code, I get the error message: "This expression causes side effects and will not be evaluated"

But if I make the same call in LinqPad in the same context, it will return the data as expected. This seems like a very simple piece of code.

Why am I getting an error? And / or how to fix this problem further, since it works in LinqPad?

+4
source share
1 answer

, , :

Order order;
if(order == null)
{
    if (reg.OrderId != null)
    {
        order = db.Orders.Where(o => o.OrderId == reg.OrderId).FirstOrDefault();
    }
    else
    {
        order = new Order();
    }
}
0

Source: https://habr.com/ru/post/1568010/


All Articles