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
}
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;
...
}
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- C# Linq query help removing foreach loops creating cleaner code
- convert foreach loop to linq code
- creating Linq to sqlite dbml from DbLinq source code
- Is it possible to access the parent of a Linq object?
- Converting foreach loop to LINQ query breaks code
- Make access possible to dynamic table LINQ EF6 Code First
- source code for LINQ 101 samples
- Data binding access in xaml vs in code behind - Linq to XML?
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Convert the code from Foreach loop to Linq
- Access to file is denied when LINQ is used instead of a foreach
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Is there a way I can do a foreach that contains code in a LINQ expression?
- Access parent property in LINQ query using c#
- c# Linq or code to extract groups from a single list of source data
- I want to convert the following code in Foreach to Linq
- LINQ - outer join to parent class in EF code first
- How to convert the following foreach loop to linq code format?
- LINQ equivalent of foreach for IEnumerable<T>
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- LINQ + Foreach vs Foreach + If
- foreach + break vs linq FirstOrDefault performance difference
- Multi-line foreach loop in linq / lambda
- Query Microsoft Access MDB Database using LINQ and C#
- Can LINQ ForEach have if statement?
- C# Groupby Linq and foreach
- c# linq create dynamic query with
- c# Linq method is not following one of my conditions that I set
- MVC 5 - While trying to delete a row using linq, there is no error and the row is not deleted. Why?
- How to access to a field of a dynamic type?
- Ruby equivalent of C# Linq Aggregate method
- Elegant way to check if a list contains an object where one property is the same, and replace only if the date of another property is later
- Joining two tables in one datagrid usig Linq C# WPF
- Uknown increment id from table [LINQ-ASP.NET-EF]
- String comparison inside a LINQ query results in a nullable booelan?
- Super slow performance of querying a DataTable with LINQ
- How can I order an IQueryable based on a field that is in another table?
- convert sql query to linq with complex left join
- Fuzzy Search on a Concatenated Full Name using NHibernate
- XDocument binding Elements and Attributes
- Help sorting XML Data in .NET (maybe with LINQ query?)
- c# Linq select join on select group by
- How can I flatten a dictionary of dictionaries and sum the values of the inner dictionary with LINQ?
- Convert HTML table to CSV using Html Agility Pack
- Sorting orderlines with different criteria in linq
- Run Queries on Models without FK/Navigational Property