0

currently I'm using DTO's and writing my Get Method, which looks like this:

public async Task<IHttpActionResult> GetOrder() {
            var order = from x in db.Order
                        select new OrderDTO {
                            OrderId = x.OrderId,
                            orderStatusCode = x.orderStatusCode,
                            OrderProducts = new List<OrderProductDTO> {
                                new OrderProductDTO {
                                   OrderId = x.OrderProducts.Select(y => y.OrderId)
                                }
                            },
                            purchaseDate = x.purchaseDate,
                            quantityOrder = x.quantityOrder,
                            totalOrderPrice = x.totalOrderPrice,
                            User = new UserDTO {
                                UserId = x.UserId,
                                username = x.User.username,
                                userInfo = new UserInfoDTO {
                                    adress = x.User.UserInfo.adress,
                                    city = x.User.UserInfo.city,
                                    country= x.User.UserInfo.country,
                                    zip  = x.User.UserInfo.zip
                                }
                            }

                        };
            return Ok(order);
        }

The problem I have is how can I initialize my list dynamically, the error i get is:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'int'.

I get why the problem occurs but dunno how to fix it, thanks. By the way OrdeProducts is a List.

1 Answer 1

1

Problem is here:

new OrderProductDTO {
   OrderId = x.OrderProducts.Select(y => y.OrderId)
}

.Select is returning IEnumerable<int>. You are trying to assign it to an int.

Instead of enumeration, get just one int:

new OrderProductDTO {
   OrderId = x.OrderProducts.Select(y => y.OrderId).FirstOrDefault()
}
Sign up to request clarification or add additional context in comments.

Comments

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.