简体   繁体   English

以正确的顺序获取数据集的子行

[英]Get dataset child rows in proper order

I have typed dataset with 1:N relationship between two tables. 我输入了两个表之间具有1:N关系的数据集。 Child table has 'Order' column, that I use for sorting (ordery by) in SP when loading data into dataset. 子表具有“订单”列,当将数据加载到数据集中时,该列用于在SP中排序(排序依据)。 As I understand, this order is not guaranteed when working with dataset. 据我了解,使用数据集时不能保证此顺序。 I need GetChildRows of parent row and ensure that rows are in specific order defined in order column. 我需要父行的GetChildRows并确保行按订单列中定义的特定顺序。 Now I do following: 现在,我执行以下操作:

class Program
{
    static void Main(string[] args)
    {
        DataSet1 ds = new DataSet1();
        //load data
        DataSet1.ParentTableRow parentRow = ds.ParentTable.FindByID(1);
        DataSet1.ChildTableRow[] childRows = parentRow.GetChildTableRows();
        Array.Sort<DataSet1.ChildTableRow>(childRows, new ChildTableCoparer());
        //Enumerate fields is right order
    }
}

public class ChildTableCoparer : IComparer<DataSet1.ChildTableRow>
{
    public int Compare(DataSet1.ChildTableRow x, DataSet1.ChildTableRow y)
    {
        if (x.Order > y.Order) return 1;
        if (x.Order < y.Order) return -1;
        return 0;
    }
}

Is there better way how to ensure order when using GetChildRows() ? 有没有更好的方法,当使用GetChildRows()时如何确保顺序?

You could try sorting the child table before you select rows from it. 您可以先尝试对子表进行排序,然后再从中选择行。 This way hopefully the array will be pre-sorted when you select rows from it. 希望这样,当您从数组中选择行时,该数组将被预先排序。

For example: 例如:

DataSet1 ds = new DataSet1();

//load data
DataSet1.ChildTable.SortExpression = "Order";

DataSet1.ParentTableRow parentRow = ds.ParentTable.FindByID(1);
DataSet1.ChildTableRow[] childRows = parentRow.GetChildTableRows();
Array.Sort<DataSet1.ChildTableRow>(childRows, new ChildTableCoparer());
//Enumerate fields is right order    

Or alternatively, you could write a LINQ expression to sort the resulting array 或者,您可以编写LINQ表达式对结果数组进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM