BIML中的Foreach数据行过滤器 [英] Foreach datarow filter in BIML

查看:56
本文介绍了BIML中的Foreach数据行过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在BIML文件中,我有2个 DataTables ,它们是通过带有SQL查询的 ExternalDataAccess.GetDataTable 调用填充的.我正在另一个内部循环,并尝试通过从外部一个值中过滤内部一个来将两个连接"在一起.

Working in a BIML file, I've got 2 DataTables that I am populating via an ExternalDataAccess.GetDataTable call with a SQL query. I am looping over one inside of the other and am trying to 'tie' the 2 together by filtering the inner one on a value from the outer one.

我已经通过编码以下代码解决了这个问题:

I've gotten around this by coding the following:

foreach (DataRow t in Target.Rows) {
    foreach (DataRow c in Columns.Rows) {
        if (c["Object"].ToString() == t["ReferenceObject"].ToString()) {
            //...
        }
    }
}

但是,我认为有一种方法可以过滤内部的 foreach 循环.我的内部 DataSet 中可能有1000条记录.这是我经验不足的地方.

However, I would think there is a way to filter the inner foreach loop. I may have 1000's of records in the inner DataSet This is where my lack of experience is shining bright.

foreach (DataRow t in Target.Rows) {
   foreach (DataRow c in Columns.Rows.Where(z => z["Object"].ToString() == t["ReferenceObject"].ToString())) {
       //...
   }
}

我得到一个错误:'System.Data.DataRowCollection'不包含'Where'的定义,也没有扩展名* ...我知道这是无效的,但这实际上是什么我正在努力做.有没有一种方法可以根据外部 foreach 循环中的值来过滤内部 foreach 循环?

I get an error: 'System.Data.DataRowCollection' does not contain a definition for 'Where' and no extension*... I know this isn't valid, but that's essentially what I'm trying to do. Is there a way to filter an inner foreach loop based on a value from the outer foreach loop?

推荐答案

要枚举表的DataRows,您需要应用

To enumerate the DataRows of a table your need to apply the AsEnumerable extension to the DataTable

foreach(DataRow row in Columns.AsEnumerable()
                .Where(z => z.Field<string>("Object") == 
                            t.Field<string>("ReferenceObject"))
 ......

但是我想探索使用

However I would like to explore the possibility to join the two tables using a DataRelation at the DataSet level if it is possible.

此处是一个MSDN示例如何浏览已为其建立 DataRelation

Here an MSDN example on how to navigate the parent/child records of two tables for which you have established a DataRelation

这篇关于BIML中的Foreach数据行过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆