I use a generic UD method that can handle a range of filtering requirements. This is using PcConData table where you have to add fields but has the advantage of having 9 keys vs the 5 keys from standard UD tables. TypeCode handles the ability to store different types of data in the same table. TypeCode = “ENGINE” or TYPECODE = “DEPTHFINDER”. The display member and value member are dynamic too.
// Enter valid C# code and do not forget this method returns a string value.
String valueMember = String.Empty;
StringBuilder MyList = new StringBuilder ();
Boolean ok = false;
var query = (from r in Db.PcConData where r.Company == Context.CompanyID
&& r.PartNum == PartNum
&& r.TypeCode == TypeCode select r);
var filterValues = new List<string> { String.Empty, Key1, Key2, Key3, Key4, Key5, Key6 };
for (int i = 1; i < 7; i++)
// start with 1 to reduce confusion or add to confusion.
{
if (!string.IsNullOrEmpty (filterValues[i]))
{
switch (i)
{
case 1:
query = query.Where (u => u.Key1 == Key1);
query = query.OrderByDescending (r => r.Key1);
// assume sort by key value
break;
case 2:
query = query.Where (u => u.Key2 == Key2);
query = query.OrderByDescending (r => r.Key2);
break;
case 3:
query = query.Where (u => u.Key3 == Key3);
query = query.OrderByDescending (r => r.Key3);
break;
case 4:
query = query.Where (u => u.Key4 == Key4);
query = query.OrderByDescending (r => r.Key4);
break;
case 5:
query = query.Where (u => u.Key5 == Key5);
query = query.OrderByDescending (r => r.Key5);
break;
case 6:
query = query.Where (u => u.Key6 == Key6);
query = query.OrderByDescending (r => r.Key6);
break;
}
}
}
switch (SortOrder)
{
case "N":
case "N1":
query = query.OrderBy (r => r.SortOrder_c); // these allow me to do a custom sort of items.
break;
case "N2":
query = query.OrderBy (r => r.SortOrder02_c);
break;
case "N3":
query = query.OrderBy (r => r.SortOrder03_c);
break;
}
valueMember = ValueMember;
ok = true;
if (ok)
{
foreach (var line in query)
{
String test = line[valueMember] + "`" + line[DisplayMember] + "~";
if (!MyList.ToString ().Contains (test)) // there is a clearer way to do this but haven't circled back to do it.
{
MyList.Append (test);
}
}
}
return ((MyList.ToString ()).Length > 0) ? (MyList.ToString ()).Substring (0, (MyList.ToString ()).Length - 1) : ("N`None");