我怎样才能从 sql 风格以流畅的风格重写这个 LINQ 查询?
How could I rewrite this LINQ query in a fluent style from sql like style?
我需要以流畅的 LINQ 风格编写此 LINQ 查询(“where result =”)。
public async Task<List<Product>> ListProductsInOrderByOrderId(int orderId)
{
var productsList = await _context.OrderProducts.Where(or => or.OrderId == orderId).ToListAsync();
var result = from prod in _context.Products
join meta in _context.OrderProducts on prod.Id equals meta.ProductId
select new Product
{
Id = prod.Id,
Name = prod.Name,
Genre = prod.Genre,
RatingByAge = prod.RatingByAge,
DateCreated = prod.DateCreated,
Price = prod.Price,
Count = prod.Count,
TotalRating = prod.TotalRating
};
var collectionOfProducts = await result.ToListAsync();
return collectionOfProducts;
}
你能帮帮我吗?
这应该有效:
var result = _context.Products.
Join(_context.OrderProducts, prod => prod.prodId, meta => meta.ProductId, (prod,meta) => new { prod, meta }).
Select(x => new Product
{
Id = x.prod.Id,
Name = x.prod.Name,
Genre = x.prod.Genre,
RatingByAge = x.prod.RatingByAge,
DateCreated = x.prod.DateCreated,
Price = x.prod.Price,
Count = x.prod.Count,
TotalRating = x.prod.TotalRating
});
products.SelectMany(prod => orderProducts.Where(meta => Equals(prod.Id, meta.ProductId))
.Select(meta => new Product
{
Id = prod.Id,
Name = prod.Name,
Genre = prod.Genre,
RatingByAge = prod.RatingByAge,
DateCreated = prod.DateCreated,
Price = prod.Price,
Count = prod.Count,
TotalRating = prod.TotalRating
}))).ToList();
我需要以流畅的 LINQ 风格编写此 LINQ 查询(“where result =”)。
public async Task<List<Product>> ListProductsInOrderByOrderId(int orderId)
{
var productsList = await _context.OrderProducts.Where(or => or.OrderId == orderId).ToListAsync();
var result = from prod in _context.Products
join meta in _context.OrderProducts on prod.Id equals meta.ProductId
select new Product
{
Id = prod.Id,
Name = prod.Name,
Genre = prod.Genre,
RatingByAge = prod.RatingByAge,
DateCreated = prod.DateCreated,
Price = prod.Price,
Count = prod.Count,
TotalRating = prod.TotalRating
};
var collectionOfProducts = await result.ToListAsync();
return collectionOfProducts;
}
你能帮帮我吗?
这应该有效:
var result = _context.Products.
Join(_context.OrderProducts, prod => prod.prodId, meta => meta.ProductId, (prod,meta) => new { prod, meta }).
Select(x => new Product
{
Id = x.prod.Id,
Name = x.prod.Name,
Genre = x.prod.Genre,
RatingByAge = x.prod.RatingByAge,
DateCreated = x.prod.DateCreated,
Price = x.prod.Price,
Count = x.prod.Count,
TotalRating = x.prod.TotalRating
});
products.SelectMany(prod => orderProducts.Where(meta => Equals(prod.Id, meta.ProductId))
.Select(meta => new Product
{
Id = prod.Id,
Name = prod.Name,
Genre = prod.Genre,
RatingByAge = prod.RatingByAge,
DateCreated = prod.DateCreated,
Price = prod.Price,
Count = prod.Count,
TotalRating = prod.TotalRating
}))).ToList();