public static ServiceAttributeCategory FromDB(dynamic record, IEnumerable <dynamic> list = null) { // Name and description can be on any of following pair of property names: var name = ""; var desc = ""; try { name = record.serviceAttributeCategoryName; desc = record.serviceAttributeCategoryDescription; } catch { name = record.name; desc = record.description; } var cat = new ServiceAttributeCategory { serviceAttributeCategoryID = record.serviceAttributeCategoryID, name = name, description = desc, requiredInput = record.requiredInput, eligibleForPackages = record.eligibleForPackages }; if (list != null) { cat.serviceAttributes = list.Select(ServiceAttribute.FromDB); } return(cat); }
/// <summary> /// Get all service attributes assigned to the given jobTitleID /// in groups indexed by the serviceAttributeCategoryID. /// </summary> /// <param name="userID"></param> /// <param name="jobTitleID"></param> /// <param name="languageID"></param> /// <param name="countryID"></param> /// <returns></returns> public static IEnumerable <ServiceAttributeCategory> GetGroupedJobTitleAttributes(int jobTitleID, int languageID, int countryID) { using (var db = new LcDatabase()) { // Get all existent attributes var data = db.Query(sqlGetJobTitleList, jobTitleID, 0, languageID, countryID); // Get all existent attributes grouped by category var catAtts = data.GroupBy(att => (int)att.serviceAttributeCategoryID, (k, l) => (ServiceAttributeCategory)ServiceAttributeCategory.FromDB(l.First(), l)); // But we still need to read all the categories that has no attributes still // and were assigned to the job-title using the special [PositionReference] field. var referenceCats = db.Query(sqlGetJobTitleReferenceCats, jobTitleID, languageID, countryID) .Select <dynamic, ServiceAttributeCategory>(c => ServiceAttributeCategory.FromDB(c, new List <dynamic>())).ToList(); // We iterate and return all the cats with attributes // while creating and index of IDs var indexCats = new List <int>(); foreach (var cat in catAtts) { yield return(cat); indexCats.Add(cat.serviceAttributeCategoryID); } // Return empty reference cats, all the ones not included in the generated index foreach (var cat in referenceCats) { if (!indexCats.Contains(cat.serviceAttributeCategoryID)) { yield return(cat); } } } }