リストから既に使用されている項目を削除して、ドロップダウン リストに入力したい
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);
利用可能なコードは、ドロップダウンリスト (id,name) の入力にのみ使用されます
this.ViewData["CodeList"] = availableCodes;