WebApi 2.0のEntityFrameWorkでDTOを使用しているので、Order内のすべてのOrdersを取得したい、OrderProductsがプログラムのリストであるため、関連するすべてのOrderProductsを取得したい今の私のコードをオーダー以下の通りです:Entity FrameWorkのダイナミックリストFrameWork WebApi 2.0
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);
すべてがOKであるように思われるが、私はWEBAPIを呼び出すときだけ最初の要素は、OrderProductでなく、すべての要素が返されます。
任意のアイデアをすべてのOrderProductsを取得する方法は?ありがとう。
なる必要があります。なぜVSからこのエラーが出るのですか? 'System.Collections.Generic.IEnumerable'を 'System.Collections.Generic.ICollection 'に暗黙的に変換できません。明示的な変換が存在します(キャストがありませんか?)。最後に.toList()に問題がない場合: –
これは別の質問です... – DavidG
ありがとう –