I've written a lengthy procedure that I call a few times to apply filters defined by the customer to a table, as I didn't see how I could turn my column name string into a LINQ column and how I could turn my action string into an action on a string in such way that the function can be used in the LINQ query like:
private void applyFilterAction(ref IQueryable<TempArticle> products, FilterAction action)
{
products = from p in products where MAGIC(p, action.Column).ToLower().MAGIC2(action.Action, action.Value) == action.AddOrKill select p;
}
I wrote this instead as a temporary solution:
private void applyFilterAction(ref IQueryable<TempArticle> products, FilterAction action)
{
var actionValue = action.Value.ToLower();
var column = action.Column;
if (action.Action.Equals("StartsWith"))
{
if (column.Equals("Description")) products = from p in products where p.Description.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("LongDescription")) products = from p in products where p.LongDescription.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Provider")) products = from p in products where p.Provider.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("ProviderCode")) products = from p in products where p.ProviderCode.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Publisher")) products = from p in products where p.Publisher.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("PublisherCode")) products = from p in products where p.PublisherCode.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom1")) products = from p in products where p.Custom1.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom2")) products = from p in products where p.Custom2.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom3")) products = from p in products where p.Custom3.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("EanCode")) products = from p in products where p.EanCode.ToLower().StartsWith(actionValue) == action.AddOrKill select p;
}
else if (action.Action.Equals("EndsWith"))
{
if (column.Equals("Description")) products = from p in products where p.Description.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("LongDescription")) products = from p in products where p.LongDescription.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Provider")) products = from p in products where p.Provider.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("ProviderCode")) products = from p in products where p.ProviderCode.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Publisher")) products = from p in products where p.Publisher.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("PublisherCode")) products = from p in products where p.PublisherCode.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom1")) products = from p in products where p.Custom1.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom2")) products = from p in products where p.Custom2.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom3")) products = from p in products where p.Custom3.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
else if (column.Equals("EanCode")) products = from p in products where p.EanCode.ToLower().EndsWith(actionValue) == action.AddOrKill select p;
}
else if (action.Action.Equals("Contains"))
{
if (column.Equals("Description")) products = from p in products where p.Description.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("LongDescription")) products = from p in products where p.LongDescription.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("Provider")) products = from p in products where p.Provider.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("ProviderCode")) products = from p in products where p.ProviderCode.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("Publisher")) products = from p in products where p.Publisher.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("PublisherCode")) products = from p in products where p.PublisherCode.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom1")) products = from p in products where p.Custom1.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom2")) products = from p in products where p.Custom2.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom3")) products = from p in products where p.Custom3.ToLower().Contains(actionValue) == action.AddOrKill select p;
else if (column.Equals("EanCode")) products = from p in products where p.EanCode.ToLower().Contains(actionValue) == action.AddOrKill select p;
}
else if (action.Action.Equals("Exact"))
{
if (column.Equals("Description")) products = from p in products where p.Description.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("LongDescription")) products = from p in products where p.LongDescription.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("Provider")) products = from p in products where p.Provider.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("ProviderCode")) products = from p in products where p.ProviderCode.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("Publisher")) products = from p in products where p.Publisher.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("PublisherCode")) products = from p in products where p.PublisherCode.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom1")) products = from p in products where p.Custom1.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom2")) products = from p in products where p.Custom2.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("Custom3")) products = from p in products where p.Custom3.ToLower().Equals(actionValue) == action.AddOrKill select p;
else if (column.Equals("EanCode")) products = from p in products where p.EanCode.ToLower().Equals(actionValue) == action.AddOrKill select p;
}
}
It filters products based on an action
object that contains:
column
: Column of the product table that needs to be filtered.action
: The action to perform on the strings coming from that column.value
: The value to compare against using the action.
Could I write this in a better way? I didn't get the switch statements in the MAGIC functions to LINQify.