2017-04-12 42 views
2

其中一個簡單的人在我的頭都受阻。我習慣於在PHP和MySQL編碼,我只是無法弄清楚在C#中的簡單語法。C#團結下拉菜單,在變化調用一個函數,函數查詢列表返回匹配列表項

我有我的項目類如下結構持有企業名單列表。我有一個團結的下拉和我的onchange在使用這個所選項目的類別ID:

private void myDropdownValueChangedHandler(Dropdown target) { 

int selectedIndex = myDropdown.value; 

    //LOAD THE ID FROM THE CATS LIST 
    string theName = myDropdown.options[selectedIndex].text; 
    var result = loadJSONCats.instance.fetchItemIDByName(theName); 

} 


public Item fetchItemByID(int id){ 

     for (int i = 0; i < myList.Count; i++) { 
      if(myList[i].ID == id){ 
       return myList[i]; 
      } 

     } 
     return null; 
} 

我現在需要尋找在MYLIST匹配列表。

在MySQL我將選擇從MYLIST *其中term_id IN(ID);

我需要一個新的列表,從這樣我就可以遍歷項目中發現的結果創建和初始化一個預製,其將與各垂直行的正確數據的垂直列表元素。

我Item類

public class Item { 

    public int ID {get; set;} 
    public string post_modified {get; set;} 
    public string post_title {get; set;} 
    public string post_type {get; set;} 
    public string guid {get; set;} 
    public string Terms_IDs {get; set;} 
    public string City {get; set;} 
    public string Latitude {get; set;} 
    public string LogoID {get; set;} 
    public string Longitude {get; set;} 

    //public Item(int id, string post_mod, string post_title, string post_type, string terms, string meta, string guid){ 
    public Item(int id, string post_mod, string post_title, string post_type, string guid, string terms, string city, string latitude, string logoID, string longitude){ 

     this.ID = id; 
     this.post_modified = post_mod; 
     this.post_title = post_title; 
     this.post_type = post_type; 
     this.guid = guid; 
     this.Terms_IDs = terms; 
     this.City = city; 
     this.Latitude = latitude; 
     this.LogoID = logoID; 
     this.Longitude = longitude; 

    } 

} 

條款ID是一個字符串的json代碼是更容易這樣翻譯。

這是我卡上的功能,

public Item findItemsByIDs(int termID){ 



} 

我需要在ID爲item.terms通過,並找到所有匹配的列表項:

public List<Item> myList = new List<Item>(); 

然後用正確的數據返回一個列表,並調用預製實例化來填充垂直網格,並在查詢中填充結果行。

我是新來的LINQ和與蘭巴之間感到困惑。

這只是其中的一個東西,我冷做很容易在SQL正常,但爲新的C#我去所有在互聯網上還是一無所獲快。

幫助讚賞。

這裏的構造函數:

void ConstructListingDatabase(){ 

     for (int i = 1; i < itemData.Count; i++) { 
      myList.Add(new Item((int)itemData[i][0], itemData[i][1].ToString(), itemData[i][2].ToString(), itemData[i][3].ToString(), itemData[i][4].ToString(), itemData[i][5].ToString(), itemData[i][6].ToString(), itemData[i][7].ToString(), itemData[i][8].ToString(), itemData[i][9].ToString())); 
     } 

    } 

回答

2

使用LINQ,你可以得到包含相同的ID

public List<Item> findItemsByIDs(int termID){ 

    return myList.Where(i => i.Terms_IDs.Split(',').Contains(termID.ToString())).ToList(); 

} 

這將返回所有Items您的列表中有Terms_IDs包含termID

的項目

別忘了添加

using System.Linq; 
+0

感謝CNuts :) – Diego

+0

@Diego不客氣:) – CNuts

+0

@Diego我編輯了我的答案,你可以試試我認爲這可能是你正在尋找的。 – CNuts

1

這是你需要的東西:

myList.Select(i => i).Where(i => int.Parse(i.Terms_IDs) == termID).ToList(); 

然後返回列表中,您需要更改方法簽名本:

public List<Item> findItemsByIDs(int termID){ 
    return myList.Select(i => i).Where(i => int.Parse(i.Terms_IDs) == termID).ToList(); 
} 
+0

非常感謝。快速跟進,如果多數民衆贊成,當我打電話功能機加工項目列表包含一個項目,但當我循環雖然沒有調試? -------------------------------------------------- ------------------------------------- \t \t //找到與CAT匹配的目錄ID \t \t List matchingItems = loadJSON.instance.findItemsByIDs(result.Term_ID); \t \t對(INT I = 0;我 Diego

+0

FormatException:輸入的字符串格式不正確 @Ousmane Mahy Diaw – Diego

+0

好了,問題出在「int.Parse()」,解決方法是確保「Terms_IDs」是一個有效的數字將「item」對象存儲到列表中。你需要在Item類中完成所有這些整理。 –