score:0
you'll need to add the parcel to the item returned by selectmany
:
parcels
.where(p => !p.owners.isnullorempty())
.selectmany(p => p.owners.select(o => new { parcel = p, owner = o }))
.orderbydescending(x => x.owner.recordingdate ?? x.owner.saledate ?? x.owner.dateentered)
.foreach(item =>
{
parcel p = item.parcel;
owner o = item.owner;
...
}
score:3
make an anonymous pair to iterate over.
foreach(var pair in parcels
.where(p => p.owners != null)
.selectmany(p => new { o = p.owners, p })
.orderbydescending(x => x.o.recordingdate ?? x.o.saledate ?? x.o.dateentered)) {
var owner = pair.o;
var parcel = pair.p;
// do stuff
}
// alternate syntax
foreach(var pair in from p in parcels
where p.owners != null
from o in p.owners
orderby o.recordingdate ?? o.saledate ?? o.dateentered descending
select new { o, p }) {
var owner = pair.o;
var parcel = pair.p;
// do stuff
}
Source: stackoverflow.com
Related Query
- How to convert the following foreach loop to linq code format?
- How are people unit testing code that uses Linq to SQL
- How to execute LINQ and/or foreach in Immediate Window in VS 2013?
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How do I convert Foreach statement into linq expression?
- How To Create Generic Data Access Object (DAO) CRUD Methods with LINQ to SQL
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How can I access the loop index inside a LINQ select?
- How to retrieve grandchild objects from a parent using linq
- LINQ method How to SelectMany with additional column from Parent class
- How can I use LINQ to project this parent and children object model into a flat, single object?
- Calling a method in a linq foreach - how much overhead is there?
- How to access associations in a LINQ query?
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- How does linq actually execute the code to retrieve data from the data source?
- How to access oracle data using Linq through ADO.net?
- LINQ Source Code Available
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- How can I combine this code into one or two LINQ queries?
- How to cast a generic T in repository<T> to an interface to access an interface property conditionally in a LINQ to SQL filter?
- linq - how do you do a query for items in one query source that are not in another one?
- How to access element from jArray with Linq
- How can I write the following code more elegantly using LINQ query syntax?
- How can I further simplify this piece of LINQ code
- How can I code an outer join using LINQ and EF6?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- linq how to select the parent from a collection where the parent contains child items in another collection
- C# Linq query help removing foreach loops creating cleaner code
More Query from same tag
- How to compare dates in LINQ?
- Find index of array with specific length in a List?
- Use value of List<int> inside IN() clause
- c# ordering strings with different formats
- Averaging every column in a DataTable
- Extension of IEnumerable's Select to include the source in the selector
- Rowspan in the view (mvc4)
- Terminology in EF and Linq
- LINQ statements in VB.net
- If using LINQ For Queries, Do We Need to Unit-Test Sorting?
- build an expression with multiple sorting
- How to count the number of elements that match a condition with LINQ
- how to check if object already exists in a list
- Using C# , LINQ how to pick items between markers again and again?
- LINQ with EF, cannot convert string to List<string>
- How can I call a local function inside a LINQ query?
- Given Array.Cast<T>(), how do I determine T via reflection?
- use linq to aggregate text file contents so that they are grouped
- Nhibernate 3 & LINQ
- How do I combine two XElements with linq select?
- How to check in neat way if collection contains an object which's one property is equal to the object passed to Contains method?
- Average or max of strings
- Why is MigrationHistory causing Linq results to differ when specifying only a subset of columns?
- Adding a condition/filter to this LINQ to XML code
- Return select objects and only desired subvalues from a list of lists using LINQ
- List View Sliding Window
- LINQ How to combine second item in Tuple after query using where on first item
- How to get product row from each order of child table list using Entity Framework Core?
- Need example of updating set data in Linq
- Lambda or Linq method to search multiple parameters