コード例 #1
0
            static StringTable Format_Table_Version_3(Unit rootUnit, string[] fields, string orderBy)
            {
                var fieldsToLower = fields.Select(s => s.ToLowerInvariant()).ToList();
                IEnumerable <string> fieldNames = fields;

                List <IPropertyDescriptor> selectedProperties;

                if (fieldsToLower.Contains("*") || fieldsToLower.Contains("all"))
                {
                    if (fieldsToLower.Count > 1)
                    {
                        selectedProperties = lowerCaseNameToProperty.OrderBy(kvp => fieldsToLower.IndexOf(kvp.Key)).Select(kvp => kvp.Value).ToList();
                    }
                    else
                    {
                        selectedProperties = allProperties;
                    }
                    fieldNames = selectedProperties.Select(p => p.Name);
                }
                else
                {
                    selectedProperties = new List <IPropertyDescriptor>();
                    for (int i = 0; i < fields.Length; i++)
                    {
                        var prop = GetOneProperty(fieldsToLower[i]);
                        if (prop != null)
                        {
                            selectedProperties.Add(prop);
                        }
                        else
                        {
                            throw new Exception($"field {fields[i]} was not found in {typeof(Person)}, possible field values are:\n{allPropertiesNames}");
                        }
                    }
                }

                var data = new StringTable();

                foreach (var f in fieldNames)
                {
                    data.AddColumn(f);
                }

                var people = rootUnit.GetAllActivePeople();

                IEnumerable <Person> persons = people;

                if (orderBy == "id")
                {
                    persons = people.OrderBy(p => p.PersonId);
                }

                foreach (var person in persons)
                {
                    var row = data.NewRow();

                    foreach (var p in selectedProperties)
                    {
                        var val = p.Read(person);
                        row.Add(val == null ? string.Empty : val.ToString());
                    }

                    data.AddRow(row);
                }

                return(data);
            }