Unity Gems: LINQ tutorial

If you want to know more about how LINQ can help you right amazingly powerful list manipulations, sorts, closest object calculations in a tiny number of lines of code - then this new tutorial will be a good introduction to a great, under utilized feature of .NET and Unity.

Available On
Unity Gems

1 Like

Congratz for this amazing website bro!!!
Your tutorials are helping me a lot on getting more “advanced” at unity3d

Thank you very very much Sir,

Before visit your site i never heard about “whydoidoit”, but now i feel that i missed lots of things regarding unity. Your tutorial regarding LINQ is very helpful for us. I also liked your Serializer package.

Again Thank you very much for all unity packages.

You are very welcome :slight_smile:

I’m really glad they are helpful - we weren’t sure that we would have a large audience when we started, but it seems that a lot of people are interested in going further with Unity!

Excellent,
I didn’t know about this site

That’s no surprise, we’re pretty new - started last month… Feel free to spread the word :wink:

Please let me know if you need any help reviewing your linq tutorials as you publish them, while i’m new to the realm of unity (6month ish) i’m fairly expert in LINQ in all its forms.

That would be great!! There’s always a cool way to do something in LINQ and as it gets more complicated a reviewer would be helpful. Actually ideas of how to use LINQ with Unity would also be helpful - I use it a lot, but I’m sure I haven’t thought of a million uses :wink:

Yours tutorial are amazing, clearly written, well organized.
Really a good and useful work.
Thanks

Ronan, I may say as well, that if you want to contribute, you may provide your own tutorials as well. You are more than welcome.

If you think you do not master English to the point you could write an article (though you seem to be ok), the website is starting a French section.

Fafase

Love reading all these articles, keep it up!

My only comment is that LINQ is notorious for being a speed bottleneck. Just be careful where/when/how you use it - like all things of course. :slight_smile:

1 Like

It’s not notorious for that, this is FUD that is being spread by people who don’t know linq.
It’s no more true than most of the “this high level feature just has to be slow” that has been plaguing software developement since the days we moved away from raw assembler programming. In most cases linq will be between 0.0000000000000000001% slower (very very slight near empty method overhead) and a “few bytes” more per operator than the best you could do without linq, in practice however it will almost always be much faster as the deffered partial execution model of linq is way way better than what you’d usually type without it. And if you’d really want to get back those tiny microseconds by typing it, you’d end up with an unmaintainble mess instead.
I’ve been routinely writing linq queries 30 to 1000 lines long (yes really) when “rewriting” components for speed and getting 10-100X speedups.

Yes - as mentioned in the tutorial it can both allocate memory and run at about 1/2 the speed of the equivalent for next loops so should rarely be used every frame in an Update loop.

In some direct tests I did - it was certainly slower than an equivalent for…next and allocated memory during execution on the heap (which requires garbage collection).

I do agree that you can frequently massively improve efficiency and memory usage using LINQ when the situation is complex and gets manual approaches get out of hand.

Wow, Ronan, you obviously feel strongly about it. It was just a warning. Too many times we’ve seen poor decisions because LINQ was easy, and doing things right was longer and harder. I’m sure all your code is very fast, and can be no faster or better. But for those that aren’t superhuman, LINQ can certainly be misused, and everyone should always consider what is appropriate when coding. I would hate to look at a 30 line LINQ query. A 1000 line query would be hell to debug. But, if you aren’t working on a team, and you have a perfect memory, then I suppose it does not matter.

@whydoidoit:

This wasn’t meant to be a criticism of your work, thanks for making tutorials, and your serialization system is very nice. Your work is appreciated.

The 1000 line query is part of an international project worked on by a multilingual team and has proven to be trivial to debug.
It’s easy to produce slow or incorrect code for a begginer with “any” API. My take is that anyone working with linq normally (including begginers) will usually produce much faster and robust code than by not using linq (talking linq to objects here, it’s a bit diferent when talking about iqueryables)
So no. I bet any begginer will produce better and more maintainable with linq than without for anything non-trivial. However i simpy felt it was important to bring an educated viewpoint on linq as there seems to be a thought that it’s inherently slow while, by design, it’s inherently faster wherever possible than any (sane and maintainable) handcrafted solution.

A free quick bump to say that i really appreciate your tutorials on surface shaders!

Many thanks :slight_smile:

On the Linq front - I think that as soon as you “get” Linq it is very easy to read - it’s much harder to debug with MonoDevelop than it is in Visual Studio running on real .NET which is a shame, but it’s not that bad if you ask me.