Referring to an entity in another context - Entity Framework

I am trying to use Any method to achieve exists functionality in Entity Framework but when I refer to another entity in the current context, I get an error

The name OrderNumber does not exists in current context

How can I fix this?

var ordResults = from ord in orders
                 select new Order
                        {
                            OrderId = ord.OrderId,
                            WarehouseId = ord.WarehouseId,
                            OrderNumber = ord.OrderNumber,
                            CustomerPoNumber = ord.CustomerPoNumber,
                            ShipToCode = ord.ShipToCode,
                            ShipToName = ord.ShipToName,
                            OrderDate = ord.OrderDate,
                            PromisedDate = ord.PromisedDate,
                            if (aadContext.TransactionLogs.Any(t => t.ControlNumber == **OrderNumber** && t.TransactionType == "670")) {"C"} else {"D"},
                            ord.Status,
                            ord.CustomerName,
                        }

1 answer

  • answered 2020-02-13 06:15 Eldho

    You could also try something like this.
    I still dont understand what {"C"} else {"D"} if its condition you could add that like
    t.somevalue=="C" || t.SomeValue=="E"

    var ordResults = from ord in orders
                 let someValue = aadContext.TransactionLogs.Any(t => t.ControlNumber == ord.OrderNumber && t.TransactionType == "670")) 
                 select new Order
                        {
                            OrderId = ord.OrderId,
                            WarehouseId = ord.WarehouseId,
                            OrderNumber = ord.OrderNumber,
                            CustomerPoNumber = ord.CustomerPoNumber,
                            ShipToCode = ord.ShipToCode,
                            ShipToName = ord.ShipToName,
                            OrderDate = ord.OrderDate,
                            PromisedDate = ord.PromisedDate,
                            DoYourValue = SomeValue
                            ord.Status,
                            ord.CustomerName,
                        }