One of those simple ones where my head is just blocked. I'm used to coding in php and mysql and I just can't figure out the simple syntax in c#.
I have a list which holds business listings as my Item class is structured below. I have a dropdown in unity and onchange I have the category ID of the selected item using this :
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;
}
I now need to look for the matching listings in mylist.
In mysql I would SELECT * from mylist where term_id IN(id);
I need a new list creating from the result so I can loop through the items found and instantiate a prefab which will be in a vertical list element with the correct data in each vertical row.
My Item class
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;
}
}
The terms IDs are a string as my json code was easier to translate that way.
This is the function I'm stuck on,
public Item findItemsByIDs(int termID){
}
I need to pass in the id for the item.terms and find all the matching list items in :
public List<Item> myList = new List<Item>();
then return a list with the right data and call the prefab instantiation to fill out a vertical grid with the rows filled by the results in the query.
I'm new to LINQ and getting confused between that and lamba.
It's just one of those things I cold do so easily in sql normally but being new to c# im going all over the internet and getting nowhere fast.
Help Appreciated.
Here's the constructor :
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()));
}
}
By using Linq you can get the Items that contain the same Id
public List<Item> findItemsByIDs(int termID){
return myList.Where(i => i.Terms_IDs.Split(',').Contains(termID.ToString())).ToList();
}
This would return all Items
in your list that have Terms_IDs
that contain termID
Don't forget to add
using System.Linq;