0

我正在使用 LINQ 的 Union 方法来组合两个或多个集合。之后,我尝试通过在集合共有的字段上调用 ​​OrderBy 来对组合集合应用排序。这是我应用排序的方式:

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

combineCollection 定义为:

Enumerable<DataRow> combinedCollection;

我需要将排序应用于整个组合集合。出于某种原因,这并没有发生。相反,我看到在组合集合中的每个“集合”块中分别对其他字段应用了排序

和想法为什么?

第一次编辑

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

}

第二次编辑

    _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;
}
4

1 回答 1

1

我认为您对 LINQ 惰性评估有疑问,我将不得不进行调查以找出导致问题的部分。

使用foreach(var item...)in 惰性函数已经咬了我(因为稍后执行时它们都引用了最后一个迭代项),但在你的情况下,这看起来不是问题。

要检查它是真正的问题,您可以在每个 LINQ 函数之后使用 aDataRow[]代替IEnumerable<DataRow>and 调用。.ToArray()

编辑:我不确定我的代码是否正确,但你不能只使用:

  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
于 2013-02-08T15:04:27.330 回答