我想从列表中删除已使用的项目以填充下拉列表
private void SetAvailableCodes(IEnumerable<ProductCodeVm> productCodes)
{
var availableCodes = from item in Enum.GetNames(typeof(ProductCodeType))
where item != ProductCodeType.None.ToString()
select new
{
Id = (int)((ProductCodeType)Enum.Parse(typeof(ProductCodeType), item)),
Name = item
};
// Todo remove productCodes.ProductCodeType
this.ViewData["CodeList"] = availableCodes;
}
public class ProductCodeVm
{
public int Id { get; set; }
public int ProductId { get; set; }
public ProductCodeType ProductCodeType { get; set; } <--- Enum
public string Value { get; set; }
}
有没有办法使用 linq 来实现这一点,或者我需要做一些转换,还是别的什么?
可用代码来自 Db
// Add codes
var codes = ProductCodeManager.GetByProductId(model.Id);
model.ProductCodes =
codes.Select(
c =>
new ProductCodeVm
{
ProductCodeType = c.ProductCodeType,
Value = c.Value,
ProductCodeId = c.ProductCodeId
});
this.SetAvailableCodes(model.ProductCodes);
可用代码仅用于填充 dropdownlist(id,name)
this.ViewData["CodeList"] = availableCodes;