tags:

views:

145

answers:

3

I'm trying to write a LINQ query and am having problems. I'm not sure if lambda expressions are the answer or not but I think they may be.

I have two combo boxes on my form: "State" and "Color".

I want to select Widgets from my database based on the values of these two dropdowns.

My widgets can be in one of the following states: Not Started, In Production, In Finishing, In Inventory, Sold. Widgets can have any color in the 'color' table in the database.

The 'state' combobox has selections "Not Sold," "In Production/Finishing", "Not Started," "In Production," "In Finishing," "In Inventory," "Sold." (I hope these are self-explanatory.)

The 'color' dropdown has "All Colors," and a separate item for each color in the database.

How can I create a LINQ query to select the widgets I want from the database based on the dropdowns?

A: 
var WidgetStateChoosen = "Sold";
//var WidgetStateChoosen = "All Widgets";
var WidgetColourChoosen = "Orange";
//var WidgetColourChoosen = "All Colours";

var widgetselected = Widgets.Where
    (w => 
        ( (WidgetStateChoosen == "All Widgets") ?  (w.WidgetState != WidgetStateChoosen) : (w.WidgetState == WidgetStateChoosen) )
        &&
        ( (WidgetColourChoosen == "All Colours") ?  (w.WidgetColour != WidgetColourChoosen) : (w.WidgetColour == WidgetColourChoosen) )
    );
Nicholas Murray
That would suffice except for the "Not Sold," "In Production/Finishing," and "All Colors" selections, all of which select for more than one value.
The Demigeek
Try the answer above by turning on and off the option parameters
Nicholas Murray
A: 

Way more code then I wish, but oh well! I wasnt sure I completely understood your state and selectionstate, but I hope my example is still helpful.

    [TestMethod]
    public void SelectionTest()
    {
        var userSelections = GetUserSelections("AllColor", (SelectedState[])Enum.GetValues(typeof(SelectedState)));
        var inventory = this.GetInventory();

        foreach (var currentSelection in userSelections)
        {
            var selection = currentSelection;
            var result = from item in inventory
                         where (item.Color == selection.Color || selection.Color == "AllColor") &&
                            this.GetStates(selection.State).Contains(item.State)
                         select item;

            Console.WriteLine("Item selected for selection: Color:{0} SelectedState:{1}", selection.Color, selection.State);

            foreach (var item in result)
            {
                Console.WriteLine("Item Color:{0};Item State:{1}", item.Color, item.State);
            }
            Console.WriteLine("");
        }
    }

    private IEnumerable<State> GetStates(SelectedState state)
    {
        var list = new List<State>();
        foreach (State currentState in Enum.GetValues(typeof(State)))
        {
            if (((int)currentState & (int)state) == (int)currentState)
            {
                list.Add(currentState);
            }
        }

        return list;
    }

    private IEnumerable<Item> GetInventory()
    {
        return new List<Item>()
                {
                    new Item() {State = State.NotStarted, Color = "Blue"},
                    new Item() {State = State.InFinishing, Color = "Red"},
                    new Item() {State = State.Sold, Color = "Yellow"},
                    new Item() {State = State.Sold, Color = "Blue"},
                    new Item() {State = State.InProduction, Color = "Blue"},
                    new Item() {State = State.InInventory, Color = "Blue"},
                };
    }

    private IEnumerable<UserSelection> GetUserSelections(String color, IEnumerable<SelectedState> states)
    {
        var list = new List<UserSelection>();

        foreach (var state in states)
        {
            list.Add(new UserSelection() { Color = color, State = state });
        }
        return list;
    }

    [Flags]
    private enum State
    {
        NotStarted = 1,
        InProduction = 2,
        InFinishing = 4,
        InInventory = 8,
        Sold = 16
    }

    private enum SelectedState
    {
        NotSold = State.InInventory, //Where does it map? I assume INInventory even if it doesnt make much sense
        InProductionOrFinishing = State.InProduction | State.InFinishing,
        NotStarted = State.NotStarted,
        InProduction = State.InProduction,
        InFinishing = State.InFinishing,
        InInventory = State.InInventory,
        Sold = State.Sold,
        SomeBizarroTrippleState = State.InProduction | State.Sold | State.NotStarted
    }

    private class UserSelection
    {
        public String Color { get; set; }
        public SelectedState State { get; set; }
    }

    private class Item
    {
        public String Color { get; set; }
        public State State { get; set; }
    }
davidwatercamp
A: 
var query = db.Widgets;

if (stateFilter == "Not sold")
    query = query.Where(w => w.State != WidgetState.Sold);
else if (stateFilter == "In Production/Finishing")
    query = query.Where(w => w.State == WidgetState.InProduction || w.State == WidgetState.Finishing);

if (colorFilter != "All colors")
    query = query.Where(w => w.Color = colorFilter);

(of course you should have a better way of testing the selected value from the combobox, testing on strings is really bad...)

Thomas Levesque