venturekasce.blogg.se

Icollections
Icollections













icollections
  1. Icollections how to#
  2. Icollections code#
  3. Icollections series#

I hope this post has given you an overview of what these methods are about, and that you got an idea of what they can be used for.Ĭertainly let me know if you have any questions or if there are any other topics you would like me to cover in a future post.The Natural History Museum, London (NHMUK) has embarked on an ambitious programme to digitise its entire collections of some 80 million specimens (see for background and details). Zip() and Join() are somewhat more complicated, but by no means less useful.Īll three of them – but especially the first two – are very specific in what they can do, so that one may not use them as often as methods such as Select() and Where(), but they are nonetheless powerful helpers to deal with merging of collections in different ways. Though simple, Concat() can be a very useful LINQ method.

icollections

Icollections code#

Lastly, there is of course value in using common and well known functionality like LINQ over writing the code yourself, since other programmers will be able to see that you are performing a join at a glance, without having to read and understand the entire block of code. We could add both of these features to our first implementation, but by then we would end up with significantly longer code than the LINQ solution allows. While that is a case we are unlikely to encounter in the specific example of units and players, we could easily find other examples, where such duplicates occur a lot.Īdditionally, Join() will also be able to handle if we encounter a unit without an associated player (and not print any corresponding tuples). If we would have multiple players with the same id, it would print additional name tuples for all units with that id, while our approach above would actually fail at runtime ( ToDictionary() will throw an exception if there are duplicate keys). However, consider that in this case, we do not have to introduce any additional variables, nor create a dictionary manually, which makes it more likely that our code will be correct.įurther, Join() can handle a lot of cases that we did not deal with in our first implementation. In this case we simply create a tuple to easily pass them on to the loop body.Īt a first glance, this may not seem clearer than the first approach above.

Icollections how to#

Note how we have to give Join() two key-selector delegates that select the keys to match – in our case u.PlayerId and p.Id.įurther, just like Zip(), we need to specify how to merge the two elements from the different input collections into one value to. I put each parameter of Join() on its own line, so make it easier to read. var a = new int ", names.Item1, names.Item2) This means that it simply appends the second sequence to the first, so we can enumerate both at once. These all take two input collections, and combine their elements into a single resulting collection in different ways.Ĭoncat() is easily the simplest of the methods we will be looking at today.Īs its name suggests this method concatenates two sequences into a single one. Specifically, I want to take a look at the following methods: Concat(), Zip() and Join().

Icollections series#

Continuing my series of posts on LINQ, today I want to write about a few of the LINQ extension methods that take multiple input collections and return a single one.















Icollections