Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

once again a silly question about lambda

Discussion in 'Scripting' started by Reedex, May 20, 2019.

  1. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    164
    this syntax is wrong... i don't know how to write it properly, can anyone? :- )
    Code (CSharp):
    1. TreasureClass_ALL = new List<string>();
    2.         TreasureClass_ALL.Add(InventoryManager.Instance.ItemContain.Weapons.Where
    3.             (x => x.ItemType == ItemType.MAINHAND).Select (x => x.ItemName));
    i need the item's name, to be put into treasureClass List.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,850
    Add adds a single item. Select returns an IEnumerable of the thing you selected (strings). So you'll want to replace Add with AddRange, as that adds all of the elements to the list.

    You could also just do this as a single line, as IEnumerables have a ToList extension from Linq.

    Code (csharp):
    1. TreasureClass_ALL = InventoryManager.Instance.ItemContain.Weapons.Where(x => x.ItemType == ItemType.MAINHAND).Select (x => x.ItemName).ToList();
     
    Reedex likes this.
  3. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    164
    Yes Thank You.
     
    Last edited: May 20, 2019