المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Can this be refractored



C# Programming
07-28-2009, 11:10 PM
So I have a DataTable that is populated from some source... Eventually the DataTable will be calls to a Database.

I have an app that allows the data to be sorted and queried in any order. So for example my DataTable lets say has Col1, Col2, Col3, and Col4. The app can request all entries for Col1.
The by this request it will also return the appropriate keys for these entries. Using these keys the app can then find all of the Col2 values listed with the associated Col1 value that you want to know.

Why do this? It builds a TreeView of data then. So something like this,

Col1-Val1
-----Col2-Val8
-----Col2-Val9
---------Col3-Val1
Col1-Val2
-----Col2-Val3
Col1-Val3

etc. etc.

Furthermore the app can switch the order. So Col1 could end up under Col3 and Col2 could be at the head of the TreeView. It all depends on the user configuration.

I acheived this by having one call in that passes in an enum, a list of input keys, and a double list of output keys, and it returns an array of the Model type.

i.e.
note: I have replace < with ^ for easier reading
public static FilterModel[] GetUniqueDataType(FilterType filter, List^int^ keys, out List^List^int^^ resultKeys)

In the call in is essentially a switch statement using the FilterType to determine what we are filtering out.

Here is where I would like to refactor. THe switch statement simply calls a Method that is almost the same in each case... its just the parameter that is filtered is different.

note: I have replace < with ^ for easier reading
public static List^string^ GetInspectionTypes(List^int^ keys, out List^List^int^^ resultKeys)
{
List^string^ uniqueList = new List^string^();
double perc;
double total = _data.Results.Rows.Count;
if (keys == null)
{
var unique = _data.Results.Select(i => i.InspectionType).Distinct();
foreach (string s in unique)
uniqueList.Add(s);
}
else
{
var results = from rows in _data.Results
where keys.Contains(rows.PK)
select rows;

var unique = results.Select(i => i.InspectionType).Distinct();
foreach (string s in unique)
uniqueList.Add(s);
}
resultKeys = GetInspectionTypeResultKeys(uniqueList);
return uniqueList;
}

private static List^List^int^^ GetInspectionTypeResultKeys(List^string^ uniqueList)
{
List^List^int^^ resultKeys = new List