private static List <ItemWithValues> LoadListFromReaderWithValues(IDataReader reader, bool getTotalRows = false) { List <ItemWithValues> itemList = new List <ItemWithValues>(); // for each distinct item i need a list of values (fieldname=>fieldvalue) try { while (reader.Read()) { ItemWithValues itemWithValues = new ItemWithValues { Item = new Item { SiteGuid = new Guid(reader["SiteGuid"].ToString()), FeatureGuid = new Guid(reader["FeatureGuid"].ToString()), ModuleGuid = new Guid(reader["ModuleGuid"].ToString()), ModuleID = Convert.ToInt32(reader["ModuleID"]), DefinitionGuid = new Guid(reader["DefinitionGuid"].ToString()), ItemGuid = new Guid(reader["ItemGuid"].ToString()), ItemID = Convert.ToInt32(reader["ItemID"]), SortOrder = Convert.ToInt32(reader["SortOrder"]), CreatedUtc = Convert.ToDateTime(reader["CreatedUtc"]), LastModUtc = Convert.ToDateTime(reader["LastModUtc"]), ViewRoles = reader["ViewRoles"].ToString(), EditRoles = reader["EditRoles"].ToString() }, Values = new Dictionary <string, object>(), //FieldGuids = new Dictionary<string, Guid>() }; // Not all methods will use TotalRows but there is no sense in having an extra method to load the reader // so, we'll catch the error and do nothing with it because we are expecting it // the if statement should keep any problems at bay but we still use try/catch in case someone inadvertently // set getTotalRows = true if (getTotalRows) { try { if (reader["TotalRows"] != DBNull.Value) { _totalRows = Convert.ToInt32(reader["TotalRows"]); } } catch (System.IndexOutOfRangeException ex) { } } try { //first, we'll try to add the value to the list with the corresponding item //itemList.Where(i => i.Item.ItemGuid == itemWithValues.Item.ItemGuid).First().Values // .Add(reader["FieldName"].ToString(), // new { GUID = reader["FieldGuid"].ToString(), Value = reader["FieldValue"].ToString() }); itemList.Where(i => i.Item.ItemGuid == itemWithValues.Item.ItemGuid).First().Values .Add(reader["FieldName"].ToString(), reader["FieldValue"].ToString()); } catch (Exception ex) { //corresponding item not found, we'll add our value to the current item and then add that item to our list //itemWithValues.Values.Add(reader["FieldName"].ToString(), // new { GUID = reader["FieldGuid"].ToString(), Value = reader["FieldValue"].ToString() }); itemWithValues.Values.Add(reader["FieldName"].ToString(), reader["FieldValue"].ToString()); itemList.Add(itemWithValues); } } } finally { reader.Close(); } return(itemList); }
//public Dictionary<string, Guid> FieldGuids { get; set; } public int CompareTo(ItemWithValues other) { return(this.Item.ItemID.CompareTo(other.Item.ItemID)); }