Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9298883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:25:52+00:00 2026-06-18T22:25:52+00:00

I’m using LINQ’s Union method to combine two or more collections. After that I’m

  • 0

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:

combinedCollection.OrderBy(row => row["common_field"]);

combinedCollection is defined as:

Enumerable<DataRow> combinedCollection;

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

And idea why??

First Edit

foreach (....)
{
    if (combinedCollection != null)
    {
        combinedCollection = combinedCollection.Union(aCollection);
    }
    else
    {
        combinedCollection = aCollection;
    }

}

Second Edit

    _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<DataRow> 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<DataRow> cityFilteredEmployeeRows = null;


    //Copy filtered rows into a data table
    DataTable filteredEmployeeTable = filteredEmployeeRows.CopyToDataTable<DataRow>();

    foreach (DataRowView city in CityListBox.SelectedItems)
    {
        // create an exact copy of the data table
        DataTable filteredEmployeeCopyTable = filteredEmployeeTable.Copy();

        // Enumerate it
        IEnumerable<DataRow> 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 => row["Age"]);
    //cityFilteredEmployeeRows.OrderByDescending(row => row["Age"]);

    EmployeeGridView.DataSource = cityFilteredEmployeeRows.CopyToDataTable<DataRow>();

…….

private IEnumerable<DataRow> _ApplyCityFilter(IEnumerable<DataRow> filteredEmployeeRows, string city)
{
    IEnumerable<DataRow> temp = filteredEmployeeRows;

    filteredEmployeeRows = from row in temp
                           where row["City"].ToString() == city
                           select row;
    return filteredEmployeeRows;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:25:53+00:00Added an answer on June 18, 2026 at 10:25 pm

    I think you have a problem with the LINQ lazy evaluation, I would have to investigate to find out wich part causes the problem.

    Using the foreach(var item...) in lazy functions has already bitten me (because when executed later they all reference the last iterated item), but in your case it doesn’t look like this is the problem.

    To check it is the really the issue you can just use a DataRow[] in place of the IEnumerable<DataRow> and call .ToArray() after every LINQ function.

    Edit: I’m not sure I got your code right but can’t you just use:

      var cities = CityListBox.SelectedItems.Cast<DataRowView>()
          .Select(city => city["City"].ToString())
          .ToArray();
    
      var rows = allRows
          .Where(r => cities.Contains((string)r["City"]))
          .OrderBy(r => (int?)r["Age"])
          .ToArray(); // if you want to evaluate directly, not mandatory
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.