LINQ查询和lambda表达式

时间:2010-04-29 20:55:26

标签: linq lambda

我正在尝试编写LINQ查询并遇到问题。我不确定lambda表达式是否是答案,但我认为它们可能是。

我的表单上有两个组合框:“State”和“Color”。

我想根据这两个下拉列表的值从我的数据库中选择小部件。

我的小工具可以处于以下状态之一:未开始,正在生产,在完成中,在库存中,已售出。窗口小部件可以在数据库的“颜色”表中具有任何颜色。

'州'组合框选择“未售出”,“生产/完成”,“未开始”,“生产中”,“完成中”,“库存中”,“已售出”。 (我希望这些是不言自明的。)

“颜色”下拉列表中包含“所有颜色”,以及数据库中每种颜色的单独项目。

如何创建LINQ查询以根据下拉列表从数据库中选择我想要的小部件?

3 个答案:

答案 0 :(得分:0)

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) )
    );

答案 1 :(得分:0)

然后我希望更多的代码,但哦,哦!我不确定我是否完全理解你的状态和选择状态,但我希望我的例子仍然有用。

    [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; }
    }

答案 2 :(得分:0)

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);

(当然你应该有更好的方法来测试组合框中的选定值,测试字符串真的很糟糕......)