C# Unity 下拉菜单,在更改调用函数时,函数查询列表返回匹配的列表项

标签 c# list linq unity-game-engine uielement

其中一个简单的问题让我的头被堵住了。我习惯了用 php 和 mysql 编写代码,只是搞不懂 c# 中的简单语法。

我有一个包含企业列表的列表,因为我的 Item 类的结构如下。我在 unity 和 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 中,我会 SELECT * from mylist where term_id IN(id);

我需要根据结果创建一个新列表,以便我可以遍历找到的项目并实例化一个预制件,该预制件将位于垂直列表元素中,每个垂直行中都包含正确的数据。

我的元素类别

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



}

我需要传入 item.terms 的 id 并在以下位置查找所有匹配的列表项:

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

然后返回一个包含正确数据的列表,并调用预制件实例化,以用查询结果填充的行来填充垂直网格。

我是 LINQ 新手,并且对 LINQ 和amba 感到困惑。

这只是我通常在 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()));
        }

  }

最佳答案

通过使用Linq,您可以获取包含相同Id的Item

public List<Item> findItemsByIDs(int termID){

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

}

这将返回列表中 Terms_IDs 包含 termID 的所有 Items

别忘了添加

using System.Linq;

关于C# Unity 下拉菜单,在更改调用函数时,函数查询列表返回匹配的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43371405/

相关文章:

c# - 在 C# 中调用 System.Delegate 类对象

c# - MVC 应用程序中的 MQTT 客户端

ios - 在Swift 2.0中填充列表并显示结果

c# - Linq 比较 2 个不同的列表并选择外部连接

c# - 自定义 Powershell Cmdlet - 创建隐藏的可访问变量

python - 如何在另一个列表的元组中插入列表元素

c# - 指定的转换无效 - double 列表到 float 列表

c# - 使用 Linq 从 C# 中的字典中删除具有相同值数据的多个键

c# - Linq 无法翻译 ToShortDateString()

c# - 将 AsyncCompletedEventHandler 设置为类外的方法