2010-04-29 186 views
0

我想寫一個LINQ查詢並遇到問題。我不確定lambda表達式是否是答案,但我認爲它們可能是。LINQ查詢和lambda表達式

我在窗體上有兩個組合框:「狀態」和「顏色」。

我想根據這兩個下拉列表的值從我的數據庫中選擇Widgets。

我的小部件可能處於以下狀態之一:未開始,正在生產,已完成,已清點,已售出。小部件可以在數據庫的'顏色'表中具有任何顏色。

「狀態」組合框具有「未售出」,「正在生產/完成」,「未開始」,「正在生產」,「正在完成」,「庫存中」,「已售出」 (我希望這些都是不言自明的。)

「顏色」下拉菜單中包含「所有顏色」,以及數據庫中每種顏色的單獨項目。

如何創建一個LINQ查詢來根據下拉列表從數據庫中選擇我想要的小部件?

回答

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

這就夠了除了「不出售,「製作/整理」和「所有顏色」選項,所有這些都選擇了多個值。 – 2010-04-29 21:48:38

+0

通過打開和關閉選項參數來嘗試上面的答案 – 2010-04-30 10:09:30

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

(當然你應該從ComboBox測試選定值的更好的方法,對字符串的測試實在是太差了...)