2

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

  }
Diego
  • 371
  • 1
  • 3
  • 13

2 Answers2

2

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;
I.B
  • 2,925
  • 1
  • 9
  • 22
  • @Diego I edited my answer could you try it I think it might be what you're looking for. – I.B Apr 12 '17 at 14:46
  • 1
    @Diego If your problem is solved, you can [accept](https://meta.stackexchange.com/a/5235) the answer. – Programmer Apr 15 '17 at 05:49
1

this is what you'll need:

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

then to return the list you'll need to change the method signature to this:

public List<Item> findItemsByIDs(int termID){
    return  myList.Select(i => i).Where(i => int.Parse(i.Terms_IDs) == termID).ToList();
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Thanks so much. Quick follow up if thats ok, When I call the function The maching items list contains an item but when I loop though nothing debugs? --------------------------------------------------------------------------------------- //FIND THE LISTINGS THAT MATCH THAT CAT ID List matchingItems =loadJSON.instance.findItemsByIDs(result.Term_ID); for (int i = 0; i < matchingItems.Count; i++) { Debug.Log(matchingItems[i].post_title); } Debug.Log("HERE"); – Diego Apr 12 '17 at 14:08
  • FormatException: Input string was not in the correct format @Ousmane Mahy Diaw – Diego Apr 12 '17 at 14:19
  • oh okay, the problem is with the "int.Parse()" , the solution to this is to make sure that "Terms_IDs" is a valid number before storing the "item" object into the list. you'll need to do all this tidying up inside the Item class. – Ousmane D. Apr 12 '17 at 14:21
  • The terms are stored as a string though as it was causing me difficulty with the json mapping to the item object. How would i change the item class in the way you mention, sorry for being a noob in c# btw – Diego Apr 12 '17 at 14:24
  • Updated with the constructor, I tried to change the item class to int[] Term_IDs and then the arguments and the constructor but messed it up so reverted back – Diego Apr 12 '17 at 14:31
  • @Diego what I am saying is I need to see the value of Terms_IDs , that actual value it contains. try to pop up a message showing "itemData[i][5].ToString()" or a console.write "itemData[i][5].ToString()". – Ousmane D. Apr 12 '17 at 14:34
  • The Json is "Terms_IDs": "248,249,250,251,252,253,254,255,256,257,258,259,260,261,262" They are the category IDs the listing is tagged in, this one has many as its a restaurant so the categories are things like, family friendly, serves alcohol etc. – Diego Apr 12 '17 at 14:36
  • you'll need to split the termsIDs into an array like this with the Split(',') delimiter. – Ousmane D. Apr 12 '17 at 14:38