Get item by id in inventory.

10/05/2011 10:43 BaussHacker#1
This is how I do it in my source.
Is it a fine way to do it?
Code:
        public Item GetItemInInventoryByID(uint ID)
        {
            var ret = from item
                      in this.Inventory
                      where item.ID == ID
                      select item;
            foreach (Item retitem in ret)
                return retitem;

            return null;
        }
10/05/2011 11:18 Korvacs#2
What the hell kind of messed up method is that.......

I assume this is a troll?

You use linq to query the Inventory object, then loop through your results to return a single item or return null?

Really??

Bad Bauss, BAD!
10/05/2011 11:25 BaussHacker#3
Ohh I didn't thought of it like that >< Close this xD
10/05/2011 11:28 -impulse-#4
Code:
 public Item GetItemInInventoryByID(uint ID)
        {
            foreach(var item in this.Inventory)
                        if(item.ID == ID)
                                    return item;
            return null;
        }
Why would you use a double loop when you can do it only with one? LINQ will get you more than 1 item, which means you can also count in creating a enumerable<> and adding the items in that list, then return it on which you loop and get the first element? :confused:

Why do you even bother using LINQ :confused:
10/05/2011 11:30 BaussHacker#5
Just close this, I feel bad now =[ LOOL
10/05/2011 11:34 Korvacs#6
Quote:
Originally Posted by BaussHacker View Post
Just close this, I feel bad now =[ LOOL
:D