score:3
as darin says, you can't easily recurse without doing so explicitly - but you can still simplify this code with linq:
static int mymethod(myclass my, bool b)
{
int cnt = my.somemethod().sum(cls => cls.length);
if (b)
{
cnt += my.someothermethod().sum(aa => mymethod(aa, true));
}
return cnt;
}
score:1
you cannot write recursive queries with linq.
score:2
first of all, you should use the solution by jon, because it is the most straightforward and readable soltion you can get. however, you can write recursive func<...>
declarations in c#, so the code could be rewritten like this:
func<myclass, bool, int> myfoo = null;
f = (my, b) =>
my.somemethod().sum(cls => cls.length) +
(b ? my.someohtermethod().sum(aa => myfoo(aa, true)) : 0);
this is essentially the same thing as what jon posted, with the exception that i'm using func<..>
instead of a full method. (this can be useful for encoding recursion in linq queries, but i think it is useful only very rarely)
Source: stackoverflow.com
Related Query
- How to convert this recursive code to Linq
- How do i convert this linq code to inline sql
- Linq - how to convert this code to linq
- How can i convert this code snippet into LINQ format?
- How do I convert this looped code to a single LINQ implementation?
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How does this linq code that splits a sequence work?
- How can I combine this code into one or two LINQ queries?
- How can I further simplify this piece of LINQ code
- How to render this map-reduce javascript code to an equivalent LINQ Select-Aggregate?
- How to convert this Linq query syntax to method syntax?
- How to convert this foreach loop into Linq code?
- How do I convert this SQL query to LINQ to SQL
- How to convert this linq query to lambda?
- How to convert this SQL query to LINQ or Lambda expression?
- How might I convert this SQL code to LINQ?
- How do I convert this to using Linq to dataset?
- Linq Puzzle: How do I convert this foreach to linq syntax?
- How to convert this LINQ query syntax that connects two objects to method syntax?
- How to convert this code to LINQ?
- How can I convert this SQL to LINQ
- How to convert this complex SQL Query with Subqueries into LINQ
- How to write this code using the Linq Extension Method-Syntax?
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- How can I refactor this code for LINQ filtering?
- How to convert this SQL to LINQ in C#?
- How to convert this to LINQ
- How do I convert this tSQL statement to LINQ using group by in a sub query
- How to convert this LEFT JOIN Linq Query to Lambda expression
- How can I convert this SQL to VB.NET LINQ
More Query from same tag
- Creating a list of objects belonging to another class using linq
- For loop in a view
- LINQ select one bool from condition over multiple rows
- Get parent object plus its child count without anonymous type
- Expression to create instance and set members not using MemberInit
- Filtering list if date is null or not using Linq query
- Store value in dictionary or retrieve from concatened key
- Combobox Display items
- Debugging a tough LINQ-to-SQL Timeout
- Data access using Entity Framework in ASP.NET MVC
- Best way to insert milions of records
- LINQ count in group from join
- cosmos db LINQ query in documentation
- How to approach querying a parent table based on values in a child table and a child tables related data table?
- Using LINQ to get one entity with the most of a child entity
- IComparable implementation for list of students and linq Max and Min methods
- Dynamic CRM :Contains<> is not working in CRM
- Composite keys and IsRequired(false)
- Set operations using a specific comparison rule
- C# Linq Column name as two words (Last year) or as number with leading zero (01)
- C# Data Structure to allow fast finding of objects in a large dataset with a property between two given doubles
- Steps for a beginner to run very basic linq to sql query using Linqpad
- Better code for avoiding one dictionary - Case Sensitivity Issue
- How to get an XML attribute value with Linq to XML?
- How to assign LINQ Query result to Custom Collection and convert List<T> to T[]
- FirstOrDefault is signicantly faster than SingleOrDefault while viewing ANTS profiler
- Get the set of grouped values as a list using linq
- Which linq functions are fully supported by NHibernate 3.0 IQueryable<T>
- Cache LINQ query - IEnumerable.Skip()
- how to create a list without duplicate and only add different data in a same primary key