我想从模型中根据“ ParamertomId”(表参数)按“值”(表产品参数)获得排序的产品(表产品)。 如何正确排序相关数据?
这些是我的模型类:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public double Amount { get; set; }
public double PrimeCostEUR { get; set; }
[ForeignKey("ProductTypeId")]
public int ProductTypeId { get; set; }
public virtual ProductType ProductType { get; set; }
public ICollection<ProductParameter> ProductParameters { get; set; } = new List<ProductParameter>();
}
public class ProductType //: BaseObject
{
public int Id { get; set; }
public string NameType { get; set; }
public ICollection<Parameter> Parameters { get; set; } = new List<Parameter>();
public ICollection<Product> Products { get; set; } = new List<Product>();
}
public class Parameter
{
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("ProductType")]
public int ProductTypeId { get; set; }
public virtual ProductType ProductType { get; set; }
public ICollection<ProductParameter> ProductParameters { get; set; } = new List<ProductParameter>();
}
public class ProductParameter
{
public int Id { get; set; }
public int ProductId { get; set; }
public virtual Product Product { get; set; }
public int ParameterId { get; set; }
public virtual Parameter Parameter { get; set; }
public string Value { get; set; }
}
这些是我的DTO类,用于显示所需的数据。
public class ProductDTO{
public int ProductId { get; set; }
public string Number { get; set; }
public double Amount { get; set; }
public double PrimeCostEUR { get; set; }
public int ProductTypeId { get; set; }
public string NameType { get; set; }
public ICollection<ParameterDTO> Parameters { get; set; } = new
List<ParameterDTO>();}
public class ParameterDTO {
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
我从三个表接收相关数据。这是我的方法GetSortedProducts:
public async Task<IEnumerable<ProductDTO>> GetProducts(int id)
{
var typeParams = _context.Parameters
.Where(t => t.ProductTypeId == id)
.ToList();
var products = _context.Products
.Include(t => t.ProductParameters)
.Where(t => t.ProductTypeId == id)
.ToList();
var items = new List<ProductDTO>();
foreach (var product in products)
{
var productDTO = new ProductDTO()
{
ProductId = product.Id,
Number = product.Number,
Amount = product.Amount,
PrimeCostEUR = product.PrimeCostEUR,
ProductTypeId = product.ProductTypeId
};
foreach (var typeParam in typeParams)
{
var paramDTO = new ParameterDTO();
var value = product.ProductParameters.FirstOrDefault(t => t.ParameterId == typeParam.Id);
if (value != null)
{
paramDTO.Id = value.Id;
paramDTO.Value = value.Value;
}
paramDTO.ParameterId = typeParam.Id;
paramDTO.Name = typeParam.Name;
productDTO.Parameters.Add(paramDTO);
}
// sort products by value
productDTO.Parameters.Where(y => y.ParameterId == 4)
.OrderBy(t => t.Value).ToList();
items.Add(productDTO);
}
return items;
}
答案 0 :(得分:1)
这不起作用,因为LINQ 从不会影响原始集合,因此这根本不起作用:
productDTO.Parameters
.Where(y => y.ParameterId == 4)
.OrderBy(t => t.Value)
.ToList();
总的来说,该代码甚至与良好的代码也不相近。不要仅将Product
(及其他)对象转换为DTO版本。正确使用实体框架:
var products = await _context.Products
.Where(prod => prod.ProductTypeId == id)
.Select(prod => new ProductDTO
{
ProductId = prod.ProductId,
// rest of properties...
// now get the parameters
Parameters = prod.Parameters
// assuming you want to order by value, the question isn't very clear
.OrderBy(par => par.Value)
.Select(par => new ParameterDTO
{
Id = par.Id,
// etc
})
.ToList()
})
.ToListAsync();
将单个数据库查询与问题代码正在执行的多个调用在数据已经在内存中进行比较。