1

In the project I'm working on, (.net Core) we are using a range slider (noUiSlider)to represent quality in a product filter.

Slider

The slider returns an array of numbers as an example [2, 4] would be returned if the slider was set to Good and Excellent and I would need to retrieve all the products that match Good, Very Good, and Excellent in quality.

Here in lies some of the unique problems with this. The products are pulled from a third party service, and I only receive the quality as the string value. So I need a way to assign a number value to the string value I receive so I can check for all products that have a quality >= to qualityArray[0] and <= to qualityArray[1]

What would be the best way to map between string to int so I can do the >= and <= check with the quality range and at the same time map the int to a string so when displaying the list of products that match the range I only have to display the string value? (Very Good)

I was imagining using an Enum or a Dictionary but I'm not sure what the advantages of either would be.

gboh
  • 147
  • 2
  • 13
  • what type of object are you using to store the returned products from the 3rd party service? And do you need to do the >= or <= quality or are other avenues open? – Isaac Morris Dec 06 '17 at 03:37
  • The object is a product class public class Product{ public double Price {get;set;} public string Website {get;set;} public string Quality {get;set;} ... } I would be open to other ideas alternatives to >= and <= for comparing quality, but the slider is required for this project. – gboh Dec 06 '17 at 11:27

3 Answers3

1

You could encapsulate a rating in its own class:

public class Rating
{
    public int Value { get; }

    private readonly IDictionary<string, int> _asValue = new Dictionary<string, int>
    {
        {"One", 1},
        {"Two", 2},
        {"Three", 3},
        {"Four", 4},
        {"Five", 5},
    };

    public Rating(string rating)
    {
        Value = _asValue[rating];
    }

    public override string ToString()
    {
        return _asValue.First(pair => pair.Value == Value).Key;
    }
}

To compare ratings you would just need to get the Value from each rating:

var r1 = new Rating("One");
var r2 = new Rating("Two");
var result = r1.Value > r2.Value;
Craig Curtis
  • 853
  • 10
  • 24
0

You need to cast the string value to an enum using Enum.Parse and then parse the enum to a string when required using Enum.ToString

Then you can say QualitiyValues.Low >= myObject.Quality <= QualityValues.Medium.

Enum String Name from Value

S. Walker
  • 2,129
  • 12
  • 30
0

Assuming your slider is a simple array you might try the follwing or something similar:

       // This will be what is returned by your slider
int[] ar = [2,4];
 Dictionary<int, string> test = new Dictionary<int, string>()
            {
                {1,"OK" },
                {2,"Good"},
                {3,"Very Good"},
                {4,"Excellent"}

            };

            List<string> choosenquality = new List<string>();
            for(int x =ar[0];x<=ar[1];x++)
            {
                choosenquality.Add(test[x]);
            }
//The following will be all the products that meet good, very good, and excellent
            IEnumerable<Productclass> tested = from x in myapps join y in choosenquality where x.Quality== y select x;
Isaac Morris
  • 336
  • 2
  • 11