Note that there are some explanatory texts on larger screens.

plurals
  1. POLink OrderBy method not taking effect after Union method
    primarykey
    data
    text
    <p>I'm using LINQ's Union method to combine two or more collections. After that I'm trying to apply sorting to the combined collection by calling OrderBy on a field that is common to the collections. Here is how I am applying sorting:</p> <pre><code>combinedCollection.OrderBy(row =&gt; row["common_field"]); </code></pre> <p>combinedCollection is defined as:</p> <pre><code>Enumerable&lt;DataRow&gt; combinedCollection; </code></pre> <p>I need the sorting to be applied to the entire combined collection. For some reason, that is not happening. Instead I see there is sorting applied on some other field separately within each 'collection' block within the combined collection</p> <p>And idea why??</p> <h1>First Edit</h1> <pre><code>foreach (....) { if (combinedCollection != null) { combinedCollection = combinedCollection.Union(aCollection); } else { combinedCollection = aCollection; } } </code></pre> <h1>Second Edit</h1> <pre><code> _Cmd.CommandText = "SELECT Person.Contact.FirstName, Person.Contact.LastName, Person.Address.City, DATEDIFF(YY, HumanResources.Employee.BirthDate, GETDATE()) AS Age" + " FROM HumanResources.EmployeeAddress INNER JOIN" + " HumanResources.Employee ON HumanResources.EmployeeAddress.EmployeeID = HumanResources.Employee.EmployeeID INNER JOIN" + " Person.Address ON HumanResources.EmployeeAddress.AddressID = Person.Address.AddressID INNER JOIN" + " Person.Contact ON HumanResources.Employee.ContactID = Person.Contact.ContactID AND HumanResources.Employee.ContactID = Person.Contact.ContactID AND " + " HumanResources.Employee.ContactID = Person.Contact.ContactID AND HumanResources.Employee.ContactID = Person.Contact.ContactID"; DataTable employeeTable = new DataTable(); _Adpt.Fill(employeeTable); DataRow[] allRows = null; allRows = employeeTable.Select(""); IEnumerable&lt;DataRow&gt; filteredEmployeeRows; filteredEmployeeRows = from row in allRows select row; // Declare a variable to hold the city-filtered rows and set it to null for now IEnumerable&lt;DataRow&gt; cityFilteredEmployeeRows = null; //Copy filtered rows into a data table DataTable filteredEmployeeTable = filteredEmployeeRows.CopyToDataTable&lt;DataRow&gt;(); foreach (DataRowView city in CityListBox.SelectedItems) { // create an exact copy of the data table DataTable filteredEmployeeCopyTable = filteredEmployeeTable.Copy(); // Enumerate it IEnumerable&lt;DataRow&gt; filteredEmployeeRowsInSingleCity = filteredEmployeeCopyTable.AsEnumerable(); // Apply the city filter filteredEmployeeRowsInSingleCity = _ApplyCityFilter(filteredEmployeeRowsInSingleCity, city["City"].ToString()); if (cityFilteredEmployeeRows != null) { // Combine the filtered rows for this city with the overall collection of rows cityFilteredEmployeeRows = cityFilteredEmployeeRows.Union(filteredEmployeeRowsInSingleCity); } else { cityFilteredEmployeeRows = filteredEmployeeRowsInSingleCity; } } //apply ordering cityFilteredEmployeeRows.OrderBy(row =&gt; row["Age"]); //cityFilteredEmployeeRows.OrderByDescending(row =&gt; row["Age"]); EmployeeGridView.DataSource = cityFilteredEmployeeRows.CopyToDataTable&lt;DataRow&gt;(); </code></pre> <p>.......</p> <pre><code>private IEnumerable&lt;DataRow&gt; _ApplyCityFilter(IEnumerable&lt;DataRow&gt; filteredEmployeeRows, string city) { IEnumerable&lt;DataRow&gt; temp = filteredEmployeeRows; filteredEmployeeRows = from row in temp where row["City"].ToString() == city select row; return filteredEmployeeRows; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload