Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AngryOrange · Feb 15, 2013 at 12:08 PM · linqlambda

Linq , lambda

Hi I have Generic GameObjects List of 10 items with different tags. I want to select two objects with "tag1", three with "tag2" and for example 1 GameObject with tag "tag3". I can do this :

 var  level1 = _allFigures.Where(go => go.tag == "BFig").Take(2).Select(go => go).ToList(); 

but how to add more different figures with different tag to one list ? It don't have to be in one line of course. I think I should use SelectMany ? I'am not sure

EDIT: I end up with something like this... Is there a better solution ?

 _figuresLevel1 = _figuresLevel1.Concat(TakeFigures(_allFigures, 2, "RFig")).
                   Concat(TakeFigures(_allFigures, 2, "BFig")).
                  Concat(TakeFigures(_allFigures,2,"YFig")).ToList();
  
 List <GameObjects> TakeFigures(List<GameObjects> figures, int figCount, string nameOfFigures)
 {
 return figures.Where(go => go.tag == nameOfFigures).Take(figCount).Select(go =>go).ToList();
 }

List "GameObjects" of course but editor is removing this "<"

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dubbreak · Feb 15, 2013 at 07:24 PM 0
Share

First, you don't need the Select portion (you're just doing the default select of the entire object, not a portion of it). So skip that.

Depends on what you want to do. You can select all items with the tags you want quite easily (just make a list of the tags you want and use that in the query), but it looks like you only want to select the fist X of one type Y of another etc (hence the take statement and you saying 2 of this 3 of that etc). Is that a requirement? If you wanted to take the same number from each tag you could probably wrap it all in one.

What in effect are you trying to do? I know no one ever wants to give up their game idea (and so are as ambiguous as possible) but could you be a little more detailed about what your list is and why you are trying to filter it this way? $$anonymous$$aybe this isn't the best approach.

Another option would be to do this in a loop and have a Dictionary that has the tags you want and how many of each.. but that seems overkill.

 List&lt;go&gt; TakeFigures(Dictionary&lt;string,int&gt; tagAndAmounts, figures)
 {
    List&lt;go&gt; filteredFigures = new List&lt;go&gt;();
 
    foreach( var kvp in tagAndAmounts)
    {
       var items = figures.Where(go => go.tag == nameOfFigures).Take(figCount).ToList();
 
       if(items!=null) filteredFigures.AddRange(items);
    }
 
 }

Not sure that's any better though.

avatar image AngryOrange · Feb 16, 2013 at 08:10 AM 0
Share

Hi dubbreak, thank you very much , I forgot about dictionary , I guess list of tag's it's a good idea too.

avatar image dubbreak · Feb 16, 2013 at 08:19 AM 0
Share

Glad to help. Yeah if you didn't have to use take to grab only a few of each then you could have a nice one liner with a list of tags.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Select random object in List using Linq 1 Answer

Sorting a List by Variable? 2 Answers

using xml, linq, and lists 0 Answers

Convert GameObject list to Transform list 1 Answer

I am following the tutorial "Hexagonal Grid", but immedietly hitting problems. Unity 4 Monodevelop doesn't seem to link Linq. How do I fix this? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges