- Home /
Grouping objects by a reference type field
Hello, everybody! I have a class that contains a hashset, like this:
class A : MonoBehavior{
public HashSet<SomeClass> hs;
}
And I need group the objects of the class A by the contents of their hs. I can do something like this:
List<A> SomeList;
var query =
from a in SomeList
group a by a.hs;
but it won't work since the HashSet is a reference type. If I make any function that compares this collection elementwise, how should I implement it in the linq-query?
Hey $$anonymous$$pyTou
I suspect you're going to need to use a lambda in your query to generate some kind of hash based on the contents of the collection.
In general you should try to avoid using Linq in Unity like the plague. I've just had to refactor a ton of code after learning that Linq and IEnumerables in $$anonymous$$ono leak memory all over the place.
Could you show some example of using lambda in this query in my context? Unfortunately, I would hardly refuse such a powerfull instrument as Linq and make so many things manually, I'm too lazy for this.
@VesuvianPrime do you have a source for the memory leak?
@$$anonymous$$pyTou, regardless if you use a query expression(what you did above) or a $$anonymous$$ethod Expression(the lambda and what a query expression is compiled/transformed to) you will be doing the same thing, but displaying it in code differently.
Query Syntax and $$anonymous$$ethod Syntax in LINQ (C#) Linq Query Expressions
Landern, sorry, but there is no any useful information for me, there is just a basics of linq. $$anonymous$$aybe, an easy solution for my question doesn't exist.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163022im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Confusion over LINQ syntax 2 Answers
[Solved] How to LINQ to ArrayList all the Transform within a Range? 1 Answer
Custom pathfinding and node-values 1 Answer