0

Im using DTO's in EntityFrameWork with WebApi 2.0 , so I want to retrieve all the Orders, within the Orders, OrderProducts is a list in my program, I want to retrieve all the OrderProducts related to that Order my code right now is the following:

public async Task < IHttpActionResult > GetOrder() {
    var order = from x in db.Order
    select new OrderDTO {
        OrderId = x.OrderId,
        UserId = x.UserId,
        orderStatusCode = x.orderStatusCode,
        OrderProducts = new List < OrderProductDTO > {
            new OrderProductDTO {
                OrderId = x.OrderProducts.Select(y = >y.OrderId).FirstOrDefault(),
                OrderProductId = x.OrderProducts.Select(y = >y.OrderProductId).FirstOrDefault(),
                ProductId = x.OrderProducts.Select(y = >y.ProductId).FirstOrDefault(),
                Product = new ProductDTO() {
                    productDesc = x.OrderProducts.Select(y = >y.Product.productDesc).FirstOrDefault(),
                    ProductId = x.OrderProducts.Select(y = >y.Product.ProductId).FirstOrDefault(),
                    productName = x.OrderProducts.Select(y = >y.Product.productName).FirstOrDefault(),
                    productPrice = x.OrderProducts.Select(y = >y.Product.productPrice).FirstOrDefault(),
                }
            }
        },
        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);

Everything appears to be ok, but when I call the WebApi only the first element is returned, not all the elements in OrderProduct: Only returns first value

Any idea how to retrieve all the OrderProducts? Thanks.

2 Answers 2

1

Well you're only populating a single item in your query. Instead, you should do this:

....
OrderProducts = x.OrderProducts.Select(op => new OrderProductDTO
{
    OrderId = op.OrderId,
    OrderProductId = op.OrderProductId,
    //etc
}
....
Sign up to request clarification or add additional context in comments.

2 Comments

I have another Question: why VS gives me this error: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<LoginRegister.Models.DTO.OrderProductDTO>' to 'System.Collections.Generic.ICollection<LoginRegister.Models.DTO.OrderProductDTO>'. An explicit conversion exists (are you missing a cast?) but if a append at the end .toList() no problems:
Thank you very much
0

It looks like you're only asking for one Product instead of a List

Product = new ProductDTO() {
                productDesc = x.OrderProducts.Select(y = >y.Product.productDesc).FirstOrDefault(),
                ProductId = x.OrderProducts.Select(y = >y.Product.ProductId).FirstOrDefault(),
                productName = x.OrderProducts.Select(y = >y.Product.productName).FirstOrDefault(),
                productPrice = x.OrderProducts.Select(y = >y.Product.productPrice).FirstOrDefault(),
            }

Should probably be

Products = new List<ProductDTO>() {...}

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.