链接OrderBy方法在Union方法之后不生效

时间:2013-02-08 10:02:04

标签: linq linq-to-sql

我正在使用LINQ的Union方法来组合两个或更多集合。之后,我试图通过在集合中常见的字段上调用OrderBy来对组合集合应用排序。以下是我如何应用排序:

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

combinedCollection定义为:

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

1 个答案:

答案 0 :(得分:1)

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

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

要检查它是否真的是问题,您可以使用DataRow[]代替IEnumerable<DataRow>并在每个LINQ函数后调用.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