简体   繁体   中英

Dynamic List in Entity FrameWork WebApi 2.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: 只返回第一个值

Any idea how to retrieve all the OrderProducts? Thanks.

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
}
....

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>() {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM