C# 对数据集中的多个数据表进行筛选

C# 对数据集中的多个数据表进行筛选,c#,.net,dataset,C#,.net,Dataset,我有一个包含多个数据表的数据集。我想显示ProductDataTable中的信息,它是数据集的中心表。但我希望能够根据周围表中的值过滤数据集 例如,我希望获得所有具有名为Width的功能(DataTable)和名为“Microsoft”的供应商(DataTable)的产品 我可以将数据表合并到一个数据视图中,但这会导致数据表之间的一对多关系出现问题。这是一个小手册,但代码应该可以工作: // Helper Functions private static List<T>

我有一个包含多个数据表的数据集。我想显示ProductDataTable中的信息,它是数据集的中心表。但我希望能够根据周围表中的值过滤数据集

例如,我希望获得所有具有名为Width的功能(DataTable)和名为“Microsoft”的供应商(DataTable)的产品


我可以将数据表合并到一个数据视图中,但这会导致数据表之间的一对多关系出现问题。

这是一个小手册,但代码应该可以工作:

    // 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);
//辅助函数
移除的私有静态列表副本(具有副本的列表列表)
{
列表=新列表();
foreach(列表中的T行具有重复项)
{
如果(!list.Contains(行))
列表。添加(行);
}
退货清单;
}
私有静态列表匹配父对象(DataTable表、字符串筛选器、字符串parentRelation)
{
列表=新列表();
DataView filteredView=新数据视图(表);
filteredView.RowFilter=过滤器;
foreach(filteredView.Table.Rows中的DataRow行)
{
list.Add(row.GetParentRow(parentRelation));
}
退货清单;
}
//过滤代码
列出ProductRowsMachingFeature=MatchingParents(productDS.Feature,
“名称=‘宽度’”,
“FK_产品特征”);
列出productRowsWithMatchingSupplier=MatchingParents(productDS.Supplier,
“名称=‘Microsoft’”,
“FK_产品供应商”);
List MatchesAll=productRowsMatchingFeature.FindAll(productRowsWithMatchingSupplier。
包含);
列出匹配产品=移除的副本(匹配两者);

您使用的是什么版本的框架?