การกรอง DataTable หลายรายการในชุดข้อมูล

ฉันมีชุดข้อมูลที่มี DataTable หลายอัน ฉันต้องการแสดงข้อมูลจาก Product DataTable ซึ่งเป็นตารางกลางของ DataSet แต่ฉันต้องการที่จะกรองชุดข้อมูลตามค่าจากตารางโดยรอบ

ตัวอย่างเช่น ฉันต้องการรับผลิตภัณฑ์ทั้งหมดที่มีคุณลักษณะ (DataTable) ชื่อความกว้าง และมีซัพพลายเออร์ (DataTable) ชื่อ "Microsoft"

ฉันสามารถรวม DataTables เข้ากับ DataView เดียวได้ แต่นั่นทำให้เกิดปัญหากับความสัมพันธ์แบบหนึ่งต่อกลุ่มระหว่าง DataTables


person Remco Eissing    schedule 08.01.2009    source แหล่งที่มา
comment
คุณใช้เฟรมเวิร์กเวอร์ชันใด?   -  person cgreeno    schedule 09.01.2009


คำตอบ (1)


นี่เป็นคู่มือเล็กน้อย แต่รหัสควรใช้งานได้:

    // Helper Functions
    private static List<T> RemoveDuplicates<T>(List<T> listWithDuplicates)
    {
        List<T> list = new List<T>();
        foreach (T row in listWithDuplicates)
        {
            if(!list.Contains(row))
                list.Add(row);
        }
        return list;
    }

    private static List<DataRow> MatchingParents(DataTable table, string filter, string parentRelation)
    {
        List<DataRow> list = new List<DataRow>();
        DataView filteredView = new DataView(table);
        filteredView.RowFilter = filter;
        foreach (DataRow row in filteredView.Table.Rows)
        {
            list.Add(row.GetParentRow(parentRelation));
        }
        return list;
    }

    // Filtering Code
    List<DataRow> productRowsMatchingFeature = MatchingParents(productDS.Feature, 
                                                                   "Name = 'Width'",
                                                                   "FK_Product_Feature");

    List<DataRow> productRowsWithMatchingSupplier = MatchingParents(productDS.Supplier,
                                                                   "Name = 'Microsoft'",
                                                                   "FK_Product_Supplier");

    List<DataRow> matchesBoth = productRowsMatchingFeature.FindAll(productRowsWithMatchingSupplier.
                                                                           Contains);

    List<DataRow> matchingProducts = RemoveDuplicates(matchesBoth);
person Brownie    schedule 09.01.2009