using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Kreta.BusinessLogic.Classes.ComboBox; using Kreta.Framework.Caching; namespace Kreta.BusinessLogic.Utils { public static class ComboBoxHelperExtensions { public static List ToComboBoxItemList(this IDictionary dictionary, List removeValueList = null, List onlyValueList = null) { var itemList = removeValueList != null ? dictionary.Where(x => !removeValueList.Contains(x.Key)) : dictionary; var itemList2 = onlyValueList != null ? itemList.Where(x => onlyValueList.Contains(x.Key)) : itemList; var result = itemList2.Select(x => new ComboBoxListItem { Value = x.Key, Text = x.Value }).ToList(); return result; } public static List ToSelectItemList(this IDictionary dictionary, List removeValueList = null, string selectedValuePart = null) { var itemList = removeValueList != null ? dictionary.Where(x => !removeValueList.Contains(x.Key)) : dictionary; var result = itemList.Select(x => new SelectListItem { Value = x.Key, Text = x.Value, Selected = !string.IsNullOrWhiteSpace(selectedValuePart) && x.Value.ToLower().IndexOf("- " + selectedValuePart.ToLower()) > -1 }).ToList(); return result; } public static List ToComboBoxItemList(this List dictionaryList, List removeValueList = null, List onlyValueList = null) { var itemList = removeValueList != null ? dictionaryList.Where(x => !removeValueList.Contains(x.Id)) : dictionaryList; var itemList2 = onlyValueList != null ? itemList.Where(x => onlyValueList.Contains(x.Id)) : itemList; var result = itemList2.Select(x => new ComboBoxListItem { Value = x.Id.ToString(), Text = x.Name }).ToList(); return result; } public static List ToSelectItemList(this List dictionaryList, List removeValueList = null, string selectedValuePart = null) { var itemList = removeValueList != null ? dictionaryList.Where(x => !removeValueList.Contains(x.Id)) : dictionaryList; var result = itemList.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name, Selected = !string.IsNullOrWhiteSpace(selectedValuePart) && x.Name.ToLower().IndexOf("- " + selectedValuePart.ToLower()) > -1 }).ToList(); return result; } } }