score:40
to understand the "display class" you have to understand closures. the lambda you pass here is a closure, a special type of method that magically drags in state from the scope of the method it's in and "closes around" it.
...except of course that there's no such thing as magic. all that state has to actually live somewhere real, somewhere that's associated with the closure method and readily available from it. and what do you call the programming pattern where you associate state directly with one or more methods?
that's right: classes. the compiler transforms the lambda into a closure class, then instantiates the class inside the hosting method so the hosting method can access the state in the class.
the only way to not have this happen is to not use closures. if this is really impacting performance, use an old-school for
loop instead of a linq expression.
score:25
how can i avoid creating this class (his instances and their garbage collecting) when using the any method?
why does the c# compiler creates this class and is there any alternative of any() i can use?
other posters already explained the why part, so the better question would be how can i avoid creation of a closure?. and the answer is simple: if lambda is using only the passed parameters and/or constants, the compiler will not create a closure. for instance:
bool anyclub() { return playercards.any(c => c.suit == cardsuit.club); }
bool anyof(cardsuit suit) { return playercards.any(c => c.suit == suit); }
the first will not create a closure while the second will.
with all that in mind, and assuming you don't want to use for/foreach loops, you can create own extension methods similar to those in system.linq.enumerable
but with additional parameters. for this particular case, something like this would work:
public static class extensions
{
public static bool any<t, targ>(this ienumerable<t> source, targ arg, func<t, targ, bool> predicate)
{
foreach (var item in source)
if (predicate(item, arg)) return true;
return false;
}
}
and change the code in question to:
var hasbigger =
playercards.any(otherplayercard,
(c, opc) => c.suit == opc.suit
&& c.getvalue() > opc.getvalue());
Source: stackoverflow.com
Related Query
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- Why did I get an exception "Cannot implicitly convert type 'bool' to 'long?'" when using the LINQ Sum method and how to fix it?
- Why does this Linq Cast Fail when using ToList?
- When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator
- Why LINQ method Any does not check Count?
- Why does using LINQ change properties when used in if
- why does this linq code get exponentially slower when applying First() to projection?
- How to avoid method length creep when using LINQ and vars
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Why does ToDictionary<K,V>() generate a compiler error when used with LINQ to SQL?
- Why cant we select any properties on the many side when we are using .Select(x=>new object) on Linq
- LINQ query does not return any results when using with where condition
- Why does my method not run when I attempt to delete a Record from my database using a strongly-typed Delete View?
- Why does my Linq query throw an exception when I try and sort on DateTime?
- When running a method that uses linq to get the palindromes from a string, why does SelectMany order the resulting strings?
- How to create nested object graphs using Linq when the objects are immutable and reference their parent
- I want to create a query that groups by 2 fields and does a count of those combined fields using Linq and C#
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Why does the C# compiler go mad on this nested LINQ query?
- Ambiguous call when using LINQ extension method on DbSet<T>
- Why LINQ to Entities does not recognize the method 'System.String ToString()?
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- Differences between IEquatable<T>, IEqualityComparer<T>, and overriding .Equals() when using LINQ on a custom object collection?
- Why does a Linq Cast<T> operation fail when I have an implicit cast defined?
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- Why do I have to copy "this" when using LINQ in a struct (and is it OK if I do)?
- Why does LINQ query throw an exception when I attempt to get a count of a type
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- How does LINQ defer execution when in a using statement
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
More Query from same tag
- How can I use Linq to include a List<myDto> in selectTableDto?
- Get specific node from Xml usin Linq
- Determine type in return Enumerable.Range()
- If else condition in multiple from in LINQ in C#
- How to ignore blank elements in linq query
- Grouping a get items between an timerange
- reorder csv by second column, case sensitive
- GroupByUntilChanged: is there a LINQ GroupBy operator that groups by SUCCESSIVE equal keys?
- LINQ OrderBy sort Problem
- How to get text from LINQ2SQL query?
- C# sorting list by two dates
- Get rid of non-digits and then get last n chars of a string in LINQ
- C# Linq, object definition does not contains a property
- Linq where based on List of integers
- Convert SQL Request to linq query c# (left outer join and inner join)
- C# - linq join select - operating on original selected objects
- How to use feature like Contains in LINQ to Entity
- Find and retrieve keyed value from anywhere in JSON string
- How can i compare table colums in a fast way, 2 tables
- LINQ single record forms or defaults if no record
- How can convert Xml code to linq
- How to express HAVING in LINQ
- Linq: assign variables in Lambda expressions
- lambda expression with aggregation function
- Combination Generator in Linq
- Adding new entry to DataContext db Cause null reference
- Doubly IGrouping
- Are there any alternatives to using List.Single to get an element with a primary key?
- LINQ: Help with linq query and contains for an IEnumerable<string>?
- Suggestions on how to optimize code for ordering by string in linq